from parser_tool import parse, get_parser

def test_np():
    grammar = get_parser("grammars/test_np.fcfg", trace=0)

    f = open("grammars/nounphrase.sample")
    for line in f:
        # remove newline
        actual_line = line[:-1]

        trees = parse(grammar, actual_line)
        assert len(trees) > 0, "Failed: %s" % actual_line
    f.close()

from parser_tool import parse, get_parser
from utils import go_over_file

grammar = get_parser("grammars/test_np.fcfg", trace=0)

def test_np_positive():
    def is_ok(sentence):
        trees = parse(grammar, sentence)
        assert len(trees) > 0, "Failed: %s" % sentence
    
    go_over_file("grammars/nounphrase.sample", is_ok)

def test_np_negative():
    """ tests to see if grammar refuses wrong samples """
    
    def is_not_ok(sentence):
        trees = parse(grammar, sentence)
        assert len(trees) == 0, "Failed: %s" % sentence

    go_over_file("grammars/nounphrase.sample.negative", is_not_ok)


Test both correct and wrong samples of noun phrase
Test both correct and wrong samples of noun phrase

Extended noun phrase test to check for the grammar refusing wrong
samples of noun phrases. Also added a utility module called
utils.py

  from parser_tool import parse, get_parser
+ from utils import go_over_file
  
- def test_np():
-     grammar = get_parser("grammars/test_np.fcfg", trace=0)
+ grammar = get_parser("grammars/test_np.fcfg", trace=0)
  
+ def test_np_positive():
+     def is_ok(sentence):
+         trees = parse(grammar, sentence)
+         assert len(trees) > 0, "Failed: %s" % sentence
+     
-     f = open("grammars/nounphrase.sample")
+     go_over_file("grammars/nounphrase.sample", is_ok)
-     for line in f:
-         # remove newline
-         actual_line = line[:-1]
  
+ def test_np_negative():
+     """ tests to see if grammar refuses wrong samples """
+     
+     def is_not_ok(sentence):
-         trees = parse(grammar, actual_line)
+         trees = parse(grammar, sentence)
-         assert len(trees) > 0, "Failed: %s" % actual_line
+         assert len(trees) == 0, "Failed: %s" % sentence
-     f.close()
  
+     go_over_file("grammars/nounphrase.sample.negative", is_not_ok)
+ 
+ 
Test both correct and wrong samples of noun phrase
## Code Before:
from parser_tool import parse, get_parser

def test_np():
    grammar = get_parser("grammars/test_np.fcfg", trace=0)

    f = open("grammars/nounphrase.sample")
    for line in f:
        # remove newline
        actual_line = line[:-1]

        trees = parse(grammar, actual_line)
        assert len(trees) > 0, "Failed: %s" % actual_line
    f.close()

## Instruction:
Test both correct and wrong samples of noun phrase
## Code After:
from parser_tool import parse, get_parser
from utils import go_over_file

grammar = get_parser("grammars/test_np.fcfg", trace=0)

def test_np_positive():
    def is_ok(sentence):
        trees = parse(grammar, sentence)
        assert len(trees) > 0, "Failed: %s" % sentence
    
    go_over_file("grammars/nounphrase.sample", is_ok)

def test_np_negative():
    """ tests to see if grammar refuses wrong samples """
    
    def is_not_ok(sentence):
        trees = parse(grammar, sentence)
        assert len(trees) == 0, "Failed: %s" % sentence

    go_over_file("grammars/nounphrase.sample.negative", is_not_ok)

