<commit_before>#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.

"""Unit tests for DeviceModel code generator.
"""

__author__ = '<EMAIL> (<NAME>)'

import cwmp_datamodel_1_3 as dm
import dm_codegen
import sys
import unittest

dm1_xml = """<dm:document xmlns:dm="urn:broadband-forum-org:cwmp:datamodel-1-2"
    xmlns:dmr="urn:broadband-forum-org:cwmp:datamodel-report-0-1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:broadband-forum-org:cwmp:datamodel-1-2 cwmp-datamodel-1-2.xsd 
                        urn:broadband-forum-org:cwmp:datamodel-report-0-1 cwmp-datamodel-report.xsd"
    spec="urn:broadband-forum-org:tr-181-2-0-1">
  <model name="DMTestModel">
    <description>A description</description>
    <object name="foo." access="readOnly" minEntries="1" maxEntries="1">
      <description>This is the foo object.</description>
      <parameter name="bar" access="readOnly">
        <description>This is the bar parameter</description>
        <syntax>
          <string>
            <size maxLength="12"/>
          </string>
        </syntax>
      </parameter>
    </object>
  </model>
</dm:document>"""

class DeviceModelCodegenTest(unittest.TestCase):
  def setUp(self):
    # for xmlwitch
    sys.path.append('..')

  def DoCodegen(self, xml):
    objdict = dict()
    root = dm.parseString(xml)
    dm_codegen.CollectObjects(objdict, root, [])
    out = []
    dm_codegen.EmitPrologue(out)
    for key, obj in sorted(objdict.items()):
      dm_codegen.EmitClassForObj(key, obj, out)
    return "".join(out)

  def testSimpleCodegen(self):
    pystr = self.DoCodegen(dm1_xml)
    self.assertTrue(len(pystr))
    try:
      exec(pystr)
      foo_obj = foo_()
    except (NameError, SyntaxError) as e:
      print(e)
      print("In generated code:")
      print(pystr)
      self.assertFalse(True)
    self.assertTrue(hasattr(foo_obj, 'p_bar'))
    self.assertFalse(hasattr(foo_obj, 'p_baz'))


if __name__ == '__main__':
  unittest.main()<commit_msg>Cleanup test flow, add test of default value.<commit_after>#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.

"""Unit tests for DeviceModel code generator.
"""

__author__ = '<EMAIL> (<NAME>)'

import cwmp_datamodel_1_3 as dm
import dm_codegen
import sys
import unittest

dm1_xml = """<dm:document xmlns:dm="urn:broadband-forum-org:cwmp:datamodel-1-2"
    xmlns:dmr="urn:broadband-forum-org:cwmp:datamodel-report-0-1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:broadband-forum-org:cwmp:datamodel-1-2 cwmp-datamodel-1-2.xsd 
                        urn:broadband-forum-org:cwmp:datamodel-report-0-1 cwmp-datamodel-report.xsd"
    spec="urn:broadband-forum-org:tr-181-2-0-1">
  <model name="DMTestModel">
    <description>A description</description>
    <object name="foo." access="readOnly" minEntries="1" maxEntries="1">
      <description>This is the foo object.</description>
      <parameter name="bar" access="readOnly">
        <description>This is the bar parameter</description>
        <syntax>
          <string/>
          <default type="object" value="BarDefault"/>
        </syntax>
      </parameter>
    </object>
  </model>
</dm:document>"""

class DeviceModelCodegenTest(unittest.TestCase):
  def setUp(self):
    sys.path.append('..')  # for xmlwitch
    pystr = self.DoCodegen(dm1_xml)
    self.assertTrue(len(pystr))
    try:
      exec(pystr, globals(), globals())
    except (NameError, SyntaxError) as e:
      print(e)
      print("In generated code:")
      print(pystr)
      self.assertFalse(True)

  def DoCodegen(self, xml):
    objdict = dict()
    root = dm.parseString(xml)
    dm_codegen.CollectObjects(objdict, root, [])
    out = []
    dm_codegen.EmitPrologue(out)
    for key, obj in sorted(objdict.items()):
      dm_codegen.EmitClassForObj(key, obj, out)
    return "".join(out)

  def MakeFoo(self):
    try:
      foo_obj = foo_()
    except SyntaxError as e:
      print(e)
      foo_obj = None
    self.assertTrue(foo_obj)
    return foo_obj

  def testCodegenProperties(self):
    foo_obj = self.MakeFoo()
    self.assertTrue(hasattr(foo_obj, 'p_bar'))
    self.assertFalse(hasattr(foo_obj, 'p_baz'))

  def testDefaultValues(self):
    foo_obj = self.MakeFoo()
    self.assertEqual(foo_obj.p_bar, "BarDefault")


if __name__ == '__main__':
  unittest.main()
{'fraction_non_alphanumeric': 0.1444520391888813, 'fraction_numerical': 0.014581909318751423, 'mean_word_length': 3.420946626384693, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4196341', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1545, 'n_words': 279}