Ejemplo n.º 1
0
 def parse_table(json_table):
     """
     table_data = {
         'headings': table.headings,
         'rows': [ list(row) for row in table.rows ]
     }
     return table_data
     """
     headings = json_table.get("headings", [])
     rows = json_table.get("rows", [])
     table = model.Table(headings, rows=rows)
     return table
Ejemplo n.º 2
0
    def test_parses_scenario_outline_with_tagged_examples2(self):
        # -- CASE: Examples with multiple tag-lines (= 2 tag-lines)
        doc = u'''
Feature: Alice

  @foo
  Scenario Outline: Bob
    Given we have <Stuff>

    @bar
    @baz
    Examples: Charly
      | Stuff      | Things   |
      | wool       | felt     |
      | cotton     | thread   |
'''.lstrip()
        feature = parser.parse_feature(doc)
        eq_(feature.name, "Alice")

        assert(len(feature.scenarios) == 1)
        scenario_outline = feature.scenarios[0]
        eq_(scenario_outline.name, "Bob")
        eq_(scenario_outline.tags, [model.Tag(u"foo", 1)])
        self.compare_steps(scenario_outline.steps, [
            ("given", "Given", "we have <Stuff>", None, None),
        ])

        table = model.Table(
            [u"Stuff", u"Things"], 0,
            [
                [u"wool", u"felt"],
                [u"cotton", u"thread"],
            ]
        )
        eq_(scenario_outline.examples[0].name, "Charly")
        eq_(scenario_outline.examples[0].table, table)
        expected_tags = [model.Tag(u"bar", 1), model.Tag(u"baz", 1)]
        eq_(scenario_outline.examples[0].tags, expected_tags)

        # -- ScenarioOutline.scenarios:
        # Inherit tags from ScenarioOutline and Examples element.
        eq_(len(scenario_outline.scenarios), 2)
        expected_tags = [
            model.Tag(u"foo", 1),
            model.Tag(u"bar", 1),
            model.Tag(u"baz", 1)
        ]
        eq_(set(scenario_outline.scenarios[0].tags), set(expected_tags))
        eq_(set(scenario_outline.scenarios[1].tags), set(expected_tags))
Ejemplo n.º 3
0
    def test_execute_steps_with_table(self):
        doc = u'''
Given a step with a table:
    | Name  | Age |
    | Alice |  12 |
    | Bob   |  23 |
Then a step passes
'''.lstrip()
        with patch('behave.step_registry.registry', self.step_registry):
            result = self.context.execute_steps(doc)
            expected_table = model.Table([u"Name", u"Age"], 0, [
                [u"Alice", u"12"],
                [u"Bob", u"23"],
            ])
            eq_(result, True)
            eq_(expected_table, ExampleSteps.table)
Ejemplo n.º 4
0
    def _build_examples_from_file(self, keyword, line):
        fn = line[len(keyword) + 1:].strip()
        with open(fn) as f:
            payload = json.load(f)
            headings = payload.get("headings", [])
            rows = payload.get("rows", [])
            table = model.Table(headings, rows=rows)

        self.examples = model.Examples(self.filename,
                                       self.line,
                                       keyword,
                                       name=fn,
                                       tags=self.tags)
        self.examples.table = table
        self.statement.examples.append(self.examples)
        self.tags = []
Ejemplo n.º 5
0
    def test_parse_steps_when_last_step_has_a_table(self):
        doc = u'''
Given a simple step
Then the last step has a final table:
    | Name   | City |
    | Alonso | Barcelona |
    | Bred   | London  |
'''.lstrip()
        steps = parser.parse_steps(doc)
        eq_(len(steps), 2)
        # -- EXPECTED STEP DATA:
        #     SCHEMA: step_type, keyword, name, text, table
        table2 = model.Table([u"Name", u"City"], 0, [
            [u"Alonso", u"Barcelona"],
            [u"Bred", u"London"],
        ])
        self.compare_steps(steps, [
            ("given", "Given", "a simple step", None, None),
            ("then", "Then", "the last step has a final table", None, table2),
        ])
Ejemplo n.º 6
0
    def test_parses_feature_with_a_scenario_outline_with_tags(self):
        doc = u'''
Feature: Stuff

  @stuff @derp
  Scenario Outline: Doing all sorts of stuff
    Given we have <Stuff>
    When we do stuff
    Then we have <Things>

    Examples: Some stuff
      | Stuff      | Things   |
      | wool       | felt     |
      | cotton     | thread   |
      | wood       | paper    |
      | explosives | hilarity |
'''.lstrip()
        feature = parser.parse_feature(doc)
        eq_(feature.name, "Stuff")

        assert(len(feature.scenarios) == 1)
        eq_(feature.scenarios[0].name, 'Doing all sorts of stuff')
        eq_(feature.scenarios[0].tags, [model.Tag(u'stuff', 1), model.Tag(u'derp', 1)])
        self.compare_steps(feature.scenarios[0].steps, [
            ('given', 'Given', 'we have <Stuff>', None, None),
            ('when', 'When', 'we do stuff', None, None),
            ('then', 'Then', 'we have <Things>', None, None),
        ])

        table = model.Table(
            [u'Stuff', u'Things'],
            0,
            [
                [u'wool', u'felt'],
                [u'cotton', u'thread'],
                [u'wood', u'paper'],
                [u'explosives', u'hilarity'],
            ]
        )
        eq_(feature.scenarios[0].examples[0].name, 'Some stuff')
        eq_(feature.scenarios[0].examples[0].table, table)
Ejemplo n.º 7
0
    def action_table(self, line):
        line = line.strip()

        if not line.startswith('|'):
            if self.examples:
                self.examples.table = self.table
                self.examples = None
            else:
                step = self.statement.steps[-1]
                step.table = self.table
                if step.name.endswith(':'):
                    step.name = step.name[:-1]
            self.table = None
            self.state = 'steps'
            return self.action_steps(line)

        cells = [cell.strip() for cell in line.split('|')[1:-1]]
        if self.table is None:
            self.table = model.Table(cells, self.line)
        else:
            if len(cells) != len(self.table.headings):
                raise ParserError("Malformed table", self.line)
            self.table.add_row(cells, self.line)
        return True
Ejemplo n.º 8
0
 def setUp(self):
     self.table = model.Table(self.HEAD, 0, self.DATA)
Ejemplo n.º 9
0
    def test_parses_feature_with_the_lot(self):
        doc = u'''
# This one's got comments too.

@derp
Feature: Stuff
  In order to test my parser
  As a test runner
  I want to run tests

  # A møøse once bit my sister
  Background:
    Given this is a test

  @fred
  Scenario: Testing stuff
    Given we are testing
    And this is only a test
    But this is an important test
    When we test with a multiline string:
      """
      Yarr, my hovercraft be full of stuff.
      Also, I be feelin' this pirate schtick be a mite overdone, me hearties.
          Also: rum.
      """
    Then we want it to work

  #These comments are everywhere man
  Scenario Outline: Gosh this is long
    Given this is <length>
    Then we want it to be <width>
    But not <height>

    Examples: Initial
      | length | width | height |
# I don't know why this one is here
      | 1      | 2     | 3      |
      | 4      | 5     | 6      |

    Examples: Subsequent
      | length | width | height |
      | 7      | 8     | 9      |

  Scenario: This one doesn't have a tag
    Given we don't have a tag
    Then we don't really mind

  @stuff @derp
  Scenario Outline: Doing all sorts of stuff
    Given we have <Stuff>
    When we do stuff with a table:
      | a | b | c | d | e |
      | 1 | 2 | 3 | 4 | 5 |
                             # I can see a comment line from here
      | 6 | 7 | 8 | 9 | 10 |
    Then we have <Things>

    Examples: Some stuff
      | Stuff      | Things   |
      | wool       | felt     |
      | cotton     | thread   |
      | wood       | paper    |
      | explosives | hilarity |
'''.lstrip()
        feature = parser.parse_feature(doc)
        eq_(feature.name, "Stuff")
        eq_(feature.tags, [model.Tag(u'derp', 1)])
        eq_(feature.description, [
            'In order to test my parser', 'As a test runner',
            'I want to run tests'
        ])

        assert (feature.background)
        self.compare_steps(feature.background.steps,
                           [('given', 'Given', 'this is a test', None, None)])

        assert (len(feature.scenarios) == 4)

        eq_(feature.scenarios[0].name, 'Testing stuff')
        eq_(feature.scenarios[0].tags, [model.Tag(u'fred', 1)])
        string = '\n'.join([
            'Yarr, my hovercraft be full of stuff.',
            "Also, I be feelin' this pirate schtick be a mite overdone, " + \
                "me hearties.",
            '    Also: rum.'
        ])
        self.compare_steps(feature.scenarios[0].steps, [
            ('given', 'Given', 'we are testing', None, None),
            ('given', 'And', 'this is only a test', None, None),
            ('given', 'But', 'this is an important test', None, None),
            ('when', 'When', 'we test with a multiline string', string, None),
            ('then', 'Then', 'we want it to work', None, None),
        ])

        eq_(feature.scenarios[1].name, 'Gosh this is long')
        eq_(feature.scenarios[1].tags, [])
        table = model.Table([u'length', u'width', u'height'], 0, [
            [u'1', u'2', u'3'],
            [u'4', u'5', u'6'],
        ])
        eq_(feature.scenarios[1].examples[0].name, 'Initial')
        eq_(feature.scenarios[1].examples[0].table, table)
        table = model.Table([u'length', u'width', u'height'], 0, [
            [u'7', u'8', u'9'],
        ])
        eq_(feature.scenarios[1].examples[1].name, 'Subsequent')
        eq_(feature.scenarios[1].examples[1].table, table)
        self.compare_steps(feature.scenarios[1].steps, [
            ('given', 'Given', 'this is <length>', None, None),
            ('then', 'Then', 'we want it to be <width>', None, None),
            ('then', 'But', 'not <height>', None, None),
        ])

        eq_(feature.scenarios[2].name, "This one doesn't have a tag")
        eq_(feature.scenarios[2].tags, [])
        self.compare_steps(feature.scenarios[2].steps, [
            ('given', 'Given', "we don't have a tag", None, None),
            ('then', 'Then', "we don't really mind", None, None),
        ])

        table = model.Table([u'Stuff', u'Things'], 0, [
            [u'wool', u'felt'],
            [u'cotton', u'thread'],
            [u'wood', u'paper'],
            [u'explosives', u'hilarity'],
        ])
        eq_(feature.scenarios[3].name, 'Doing all sorts of stuff')
        eq_(feature.scenarios[3].tags,
            [model.Tag(u'stuff', 1),
             model.Tag(u'derp', 1)])
        eq_(feature.scenarios[3].examples[0].name, 'Some stuff')
        eq_(feature.scenarios[3].examples[0].table, table)
        table = model.Table([u'a', u'b', u'c', u'd', u'e'], 0, [
            [u'1', u'2', u'3', u'4', u'5'],
            [u'6', u'7', u'8', u'9', u'10'],
        ])
        self.compare_steps(feature.scenarios[3].steps, [
            ('given', 'Given', 'we have <Stuff>', None, None),
            ('when', 'When', 'we do stuff with a table', None, table),
            ('then', 'Then', 'we have <Things>', None, None),
        ])
Ejemplo n.º 10
0
    for cell in row:
        v = nbv.parse(cell)
        if v is None:
            raise Exception('Unable to parse %s' % cell)
        list.append(v)
    return Row(list)


if __name__ == '__main__':
    headings = ['m', 'r', 'n']
    rows = [
        ['1', '2', '3'],
        ['()', '-->', '()'],
        ['("vid")', '<-[:e "1" -> "2" @-1 {p1: 0, p2: [1, 2, 3]}]-', '()'],
        ['<()-->()<--()>', '()', '"prop"'],
        ['EMPTY', 'NULL', 'BAD_TYPE'],
    ]
    expected = DataSet(column_names = headings,\
            rows = [
                Row([Value(iVal=1), Value(iVal=2), Value(iVal=3)]),
                Row([Value(vVal=Vertex()), Value(eVal=Edge(type=1)), Value(vVal=Vertex())]),
                Row([Value(vVal=Vertex('vid')), Value(eVal=Edge(name='e',type=-1,src='1',dst='2',ranking=-1,props={'p1': Value(iVal=0), 'p2': Value(lVal=List([Value(iVal=1),Value(iVal=2),Value(iVal=3)]))})), Value(vVal=Vertex())]),
                Row([Value(pVal=Path(src=Vertex(),steps=[Step(type=1,dst=Vertex()),Step(type=-1,dst=Vertex())])), Value(vVal=Vertex()), Value(sVal='prop')]),
                Row([Value(), Value(nVal=NullType.__NULL__), Value(nVal=NullType.BAD_TYPE)])
            ])

    table = bh.Table(headings=headings, rows=rows)
    dataset = parse(table)
    assert dataset == expected,\
                    "Parsed DataSet doesn't match, \nexpected: %s, \nactual: %s" % (expected, dataset)