Commit 2104fb27 authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Auto-format Python tests for mojom parser

These tests haven't passed in a long time and a subsequent CL will make
them pass. This CL formats them first to reduce noise in the subsequent
CL.

Files which are completely obsolete or not relevant to getting tests
passing are also deleted here.

No functional changes. Only file deletion or automated formatting from
yapf --style=mojo/public/tools/.style.yapf

Bug: 1060473
Change-Id: I312886956e74dd42f1f0deb474d44abeacdb61b5
Tbr: oksamyt@chromium.org
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2100012Reviewed-by: default avatarKen Rockot <rockot@google.com>
Commit-Queue: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#749520}
parent 01c3c35e
......@@ -9,6 +9,7 @@ import sys
import tempfile
import unittest
def _GetDirAbove(dirname):
"""Returns the directory "above" this file containing |dirname| (which must
also be "above" this file)."""
......@@ -19,6 +20,7 @@ def _GetDirAbove(dirname):
if tail == dirname:
return path
try:
imp.find_module("mojom")
except ImportError:
......@@ -27,7 +29,6 @@ from mojom import fileutil
class FileUtilTest(unittest.TestCase):
def testEnsureDirectoryExists(self):
"""Test that EnsureDirectoryExists fuctions correctly."""
......
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import imp
import os.path
import sys
import unittest
def _GetDirAbove(dirname):
"""Returns the directory "above" this file containing |dirname| (which must
also be "above" this file)."""
path = os.path.abspath(__file__)
while True:
path, tail = os.path.split(path)
assert tail
if tail == dirname:
return path
try:
imp.find_module("mojom")
except ImportError:
sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib"))
from mojom.generate import data
from mojom.generate import module as mojom
class DataTest(unittest.TestCase):
def testStructDataConversion(self):
"""Tests that a struct can be converted from data."""
module = mojom.Module('test_module', 'test_namespace')
struct_data = {
'name': 'SomeStruct',
'enums': [],
'constants': [],
'fields': [
{'name': 'field1', 'kind': 'i32'},
{'name': 'field2', 'kind': 'i32', 'ordinal': 10},
{'name': 'field3', 'kind': 'i32', 'default': 15}]}
struct = data.StructFromData(module, struct_data)
struct.fields = map(lambda field:
data.StructFieldFromData(module, field, struct), struct.fields_data)
self.assertEquals(struct_data, data.StructToData(struct))
def testUnionDataConversion(self):
"""Tests that a union can be converted from data."""
module = mojom.Module('test_module', 'test_namespace')
union_data = {
'name': 'SomeUnion',
'fields': [
{'name': 'field1', 'kind': 'i32'},
{'name': 'field2', 'kind': 'i32', 'ordinal': 10}]}
union = data.UnionFromData(module, union_data)
union.fields = map(lambda field:
data.UnionFieldFromData(module, field, union), union.fields_data)
self.assertEquals(union_data, data.UnionToData(union))
def testImportFromDataNoMissingImports(self):
"""Tests that unions, structs, interfaces and enums are imported."""
module = mojom.Module('test_module', 'test_namespace')
imported_module = mojom.Module('import_module', 'import_namespace')
#TODO(azani): Init values in module.py.
#TODO(azani): Test that values are imported.
imported_module.values = {}
imported_data = {'module' : imported_module}
struct = mojom.Struct('TestStruct', module=module)
imported_module.kinds[struct.spec] = struct
union = mojom.Union('TestUnion', module=module)
imported_module.kinds[union.spec] = union
interface = mojom.Interface('TestInterface', module=module)
imported_module.kinds[interface.spec] = interface
enum = mojom.Enum('TestEnum', module=module)
imported_module.kinds[enum.spec] = enum
data.ImportFromData(module, imported_data)
# Test that the kind was imported.
self.assertIn(struct.spec, module.kinds)
self.assertEquals(struct.name, module.kinds[struct.spec].name)
self.assertIn(union.spec, module.kinds)
self.assertEquals(union.name, module.kinds[union.spec].name)
self.assertIn(interface.spec, module.kinds)
self.assertEquals(interface.name, module.kinds[interface.spec].name)
self.assertIn(enum.spec, module.kinds)
self.assertEquals(enum.name, module.kinds[enum.spec].name)
# Test that the imported kind is a copy and not the original.
self.assertIsNot(struct, module.kinds[struct.spec])
self.assertIsNot(union, module.kinds[union.spec])
self.assertIsNot(interface, module.kinds[interface.spec])
self.assertIsNot(enum, module.kinds[enum.spec])
def testImportFromDataNoExtraneousImports(self):
"""Tests that arrays, maps and interface requests are not imported."""
module = mojom.Module('test_module', 'test_namespace')
imported_module = mojom.Module('import_module', 'import_namespace')
#TODO(azani): Init values in module.py.
imported_module.values = {}
imported_data = {'module' : imported_module}
array = mojom.Array(mojom.INT16, length=20)
imported_module.kinds[array.spec] = array
map_kind = mojom.Map(mojom.INT16, mojom.INT16)
imported_module.kinds[map_kind.spec] = map_kind
interface = mojom.Interface('TestInterface', module=module)
imported_module.kinds[interface.spec] = interface
interface_req = mojom.InterfaceRequest(interface)
imported_module.kinds[interface_req.spec] = interface_req
data.ImportFromData(module, imported_data)
self.assertNotIn(array.spec, module.kinds)
self.assertNotIn(map_kind.spec, module.kinds)
self.assertNotIn(interface_req.spec, module.kinds)
def testNonInterfaceAsInterfaceRequest(self):
"""Tests that a non-interface cannot be used for interface requests."""
module = mojom.Module('test_module', 'test_namespace')
interface = mojom.Interface('TestInterface', module=module)
method_dict = {
'name': 'Foo',
'parameters': [{'name': 'foo', 'kind': 'r:i32'}],
}
with self.assertRaises(Exception) as e:
data.MethodFromData(module, method_dict, interface)
self.assertEquals(e.exception.__str__(),
'Interface request requires \'i32\' to be an interface.')
def testNonInterfaceAsAssociatedInterface(self):
"""Tests that a non-interface type cannot be used for associated
interfaces."""
module = mojom.Module('test_module', 'test_namespace')
interface = mojom.Interface('TestInterface', module=module)
method_dict = {
'name': 'Foo',
'parameters': [{'name': 'foo', 'kind': 'asso:i32'}],
}
with self.assertRaises(Exception) as e:
data.MethodFromData(module, method_dict, interface)
self.assertEquals(
e.exception.__str__(),
'Associated interface requires \'i32\' to be an interface.')
......@@ -7,6 +7,7 @@ import os.path
import sys
import unittest
def _GetDirAbove(dirname):
"""Returns the directory "above" this file containing |dirname| (which must
also be "above" this file)."""
......@@ -17,6 +18,7 @@ def _GetDirAbove(dirname):
if tail == dirname:
return path
try:
imp.find_module("mojom")
except ImportError:
......@@ -30,12 +32,12 @@ class StringManipulationTest(unittest.TestCase):
def testToCamel(self):
self.assertEquals("CamelCase", generator.ToCamel("camel_case"))
self.assertEquals("CAMELCASE", generator.ToCamel("CAMEL_CASE"))
self.assertEquals("camelCase", generator.ToCamel("camel_case",
lower_initial=True))
self.assertEquals("CamelCase", generator.ToCamel("camel case",
dilimiter=' '))
self.assertEquals("camelCase",
generator.ToCamel("camel_case", lower_initial=True))
self.assertEquals("CamelCase", generator.ToCamel(
"camel case", dilimiter=' '))
self.assertEquals("CaMelCaSe", generator.ToCamel("caMel_caSe"))
if __name__ == "__main__":
unittest.main()
......@@ -7,6 +7,7 @@ import os.path
import sys
import unittest
def _GetDirAbove(dirname):
"""Returns the directory "above" this file containing |dirname| (which must
also be "above" this file)."""
......@@ -17,6 +18,7 @@ def _GetDirAbove(dirname):
if tail == dirname:
return path
try:
imp.find_module("mojom")
except ImportError:
......@@ -25,7 +27,6 @@ from mojom.generate import module as mojom
class ModuleTest(unittest.TestCase):
def testNonInterfaceAsInterfaceRequest(self):
"""Tests that a non-interface cannot be used for interface requests."""
module = mojom.Module('test_module', 'test_namespace')
......
......@@ -92,13 +92,13 @@ class PackTest(unittest.TestCase):
ordinal order and pack order for fields are all different.
"""
struct = mojom.Struct('test')
struct.AddField('field_3', mojom.BOOL, ordinal=3,
attributes={'MinVersion': 3})
struct.AddField(
'field_3', mojom.BOOL, ordinal=3, attributes={'MinVersion': 3})
struct.AddField('field_0', mojom.INT32, ordinal=0)
struct.AddField('field_1', mojom.INT64, ordinal=1,
attributes={'MinVersion': 2})
struct.AddField('field_2', mojom.INT64, ordinal=2,
attributes={'MinVersion': 3})
struct.AddField(
'field_1', mojom.INT64, ordinal=1, attributes={'MinVersion': 2})
struct.AddField(
'field_2', mojom.INT64, ordinal=2, attributes={'MinVersion': 3})
ps = pack.PackedStruct(struct)
versions = pack.GetVersionInfo(ps)
......
......@@ -7,6 +7,7 @@ import os.path
import sys
import unittest
def _GetDirAbove(dirname):
"""Returns the directory "above" this file containing |dirname| (which must
also be "above" this file)."""
......@@ -17,6 +18,7 @@ def _GetDirAbove(dirname):
if tail == dirname:
return path
try:
imp.find_module("mojom")
except ImportError:
......
......@@ -137,7 +137,7 @@ class ConditionalFeaturesTest(unittest.TestCase):
def testFilterStruct(self):
"""Test that definitions are correctly filtered from a Struct."""
struct_source = """
struct_source = """
struct MyStruct {
[EnableIf=blue]
enum MyEnum {
......@@ -225,8 +225,9 @@ class ConditionalFeaturesTest(unittest.TestCase):
"""
definition = parser.Parse(source, "my_file.mojom")
self.assertRaises(conditional_features.EnableIfError,
conditional_features.RemoveDisabledDefinitions,
definition, ENABLED_FEATURES)
conditional_features.RemoveDisabledDefinitions,
definition, ENABLED_FEATURES)
if __name__ == '__main__':
unittest.main()
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Simple testing utility to just run the mojom parser."""
from __future__ import print_function
import os.path
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
os.path.pardir, os.path.pardir))
from mojom.parse.parser import Parse, ParseError
def main(argv):
if len(argv) < 2:
print("usage: %s filename" % argv[0])
return 0
for filename in argv[1:]:
with open(filename) as f:
print("%s:" % filename)
try:
print(Parse(f.read(), filename))
except ParseError, e:
print(e)
return 1
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Simple testing utility to just run the mojom translate stage."""
from __future__ import print_function
import os.path
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
os.path.pardir, os.path.pardir))
from mojom.parse.parser import Parse
from mojom.parse.translate import Translate
def main(argv):
if len(argv) < 2:
print("usage: %s filename" % sys.argv[0])
return 1
for filename in argv[1:]:
with open(filename) as f:
print("%s:" % filename)
print(Translate(
Parse(f.read(), filename),
os.path.splitext(os.path.basename(filename))[0]))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
......@@ -7,6 +7,7 @@ import os.path
import sys
import unittest
def _GetDirAbove(dirname):
"""Returns the directory "above" this file containing |dirname| (which must
also be "above" this file)."""
......@@ -17,6 +18,7 @@ def _GetDirAbove(dirname):
if tail == dirname:
return path
try:
imp.find_module("mojom")
except ImportError:
......@@ -46,34 +48,45 @@ class TranslateTest(unittest.TestCase):
def testTranslateSimpleUnions(self):
"""Makes sure that a simple union is translated correctly."""
tree = ast.Mojom(
None,
ast.ImportList(),
[ast.Union("SomeUnion", None, ast.UnionBody(
[ast.UnionField("a", None, None, "int32"),
ast.UnionField("b", None, None, "string")]))])
tree = ast.Mojom(None, ast.ImportList(), [
ast.Union(
"SomeUnion", None,
ast.UnionBody([
ast.UnionField("a", None, None, "int32"),
ast.UnionField("b", None, None, "string")
]))
])
expected = [{
"name": "SomeUnion",
"fields": [{"kind": "i32", "name": "a"},
{"kind": "s", "name": "b"}]}]
"name":
"SomeUnion",
"fields": [{
"kind": "i32",
"name": "a"
}, {
"kind": "s",
"name": "b"
}]
}]
actual = translate.Translate(tree, "mojom_tree")
self.assertEquals(actual["unions"], expected)
def testMapTreeForTypeRaisesWithDuplicate(self):
"""Verifies _MapTreeForType() raises when passed two values with the same
name."""
methods = [ast.Method('dup', None, None, ast.ParameterList(), None),
ast.Method('dup', None, None, ast.ParameterList(), None)]
methods = [
ast.Method('dup', None, None, ast.ParameterList(), None),
ast.Method('dup', None, None, ast.ParameterList(), None)
]
self.assertRaises(Exception, translate._MapTreeForType,
(lambda x: x, methods, '', 'scope'))
def testAssociatedKinds(self):
"""Tests type spec translation of associated interfaces and requests."""
# pylint: disable=W0212
self.assertEquals(translate._MapKind("asso<SomeInterface>?"),
"?asso:x:SomeInterface")
self.assertEquals(translate._MapKind("asso<SomeInterface&>?"),
"?asso:r:x:SomeInterface")
self.assertEquals(
translate._MapKind("asso<SomeInterface>?"), "?asso:x:SomeInterface")
self.assertEquals(
translate._MapKind("asso<SomeInterface&>?"), "?asso:r:x:SomeInterface")
if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment