Skip to content

Commit

Permalink
Improve parsing tests on primary_attr blocks
Browse files Browse the repository at this point in the history
Increases the number and kind of tests run on tokenizing and parsing Primary
Attribute blocks.
  • Loading branch information
sirosen committed Jul 28, 2014
1 parent dc9355f commit b702437
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 6 deletions.
3 changes: 3 additions & 0 deletions tests/unit/reader/files/invalid8.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# bare identifier fails

directory
5 changes: 5 additions & 0 deletions tests/unit/reader/files/valid4.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# a single file block with a primary attr

file "/d/e/f/g" {
source /a/b/c
}
8 changes: 8 additions & 0 deletions tests/unit/reader/files/valid5.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# primary_attr block, followed by non-primary_attr block

manifest "man man"

file {
source "potato"
target "mango"
}
9 changes: 9 additions & 0 deletions tests/unit/reader/files/valid6.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# multiple Primary Attribute style blocks

manifest "man man"

file "bat man"

directory "super man"

manifest "wo man"
3 changes: 3 additions & 0 deletions tests/unit/reader/files/valid7.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# primary attr block with empty body

manifest "man man" {}
5 changes: 5 additions & 0 deletions tests/unit/reader/files/valid8.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# primary_attr block with other attrs

file "lobster" {
source "salad"
}
41 changes: 38 additions & 3 deletions tests/unit/reader/parse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
from nose.tools import istest
from os.path import dirname, join as pjoin

import salve.block.file_block
from salve.reader import parse
from salve.reader.tokenize import Token
from salve.util.context import FileContext
from salve.util import locations

from tests.utils.exceptions import ensure_except

import salve.block.file_block
import salve.block.manifest_block

_testfile_dir = pjoin(dirname(__file__), 'files')
dummy_context = FileContext('no such file')

Expand Down Expand Up @@ -217,11 +219,44 @@ def attribute_with_spaces():
def file_primary_attr_assigned():
"""
Unit: Parser File Block Primary Attr
Checks that parsing an attribute that contains spaces in quotes
does not raise an error and correctly assigns to the attribute.
Checks that parsing a Primary Attribute style file block does not raise any
errors.
"""
blocks = parse_filename(get_full_path('valid4.manifest'))
assert len(blocks) == 1
assert isinstance(blocks[0], salve.block.file_block.FileBlock)
assert len(blocks[0].attrs) == 2
assert blocks[0].get(blocks[0].primary_attr) == "/d/e/f/g"


@istest
def primary_attr_followed_by_block():
"""
Unit: Parser Primary Attribute Block Followed By Normal Block
Checks that there are no errors parsing a Primary Attribute style block
followed by an ordinary block.
"""
blocks = parse_filename(get_full_path('valid5.manifest'))
assert len(blocks) == 2
assert isinstance(blocks[0], salve.block.manifest_block.ManifestBlock)
assert len(blocks[0].attrs) == 1
assert blocks[0].get(blocks[0].primary_attr) == "man man"
assert isinstance(blocks[1], salve.block.file_block.FileBlock)
assert len(blocks[1].attrs) == 2
assert blocks[1].get('source') == "potato"
assert blocks[1].get('target') == "mango"


@istest
def file_primary_attr_with_body():
"""
Unit: Parser File Block Primary Attr And Block Body
Checks that parsing is successful on a primary attr block with a nonempty
body.
"""
blocks = parse_filename(get_full_path('valid8.manifest'))
assert len(blocks) == 1
assert isinstance(blocks[0], salve.block.file_block.FileBlock)
assert len(blocks[0].attrs) == 2
assert blocks[0].get(blocks[0].primary_attr) == "lobster"
assert blocks[0].get('source') == "salad"
32 changes: 29 additions & 3 deletions tests/unit/reader/tokenize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ def missing_open():


@istest
def double_identifier():
def missing_open_primary_attr():
"""
Unit: Tokenizer Double Identifier Fails
Ensures that two successive block ids raise a TokenizationException.
Unit: Tokenizer Missing Block Open (Primary Attribute Block) Fails
Ensures that a missing { raises a TokenizationException even on block's
with a Primary Attribute setting.
"""
ensure_TokenizationException('invalid3.manifest')

Expand All @@ -77,6 +78,16 @@ def missing_attribute_value():
ensure_TokenizationException('invalid5.manifest')


@istest
def bare_identifier():
"""
Unit: Tokenizer Bare Identifier Fails
Ensures that a block without body or primary attr value raises a
TokenizationException.
"""
ensure_TokenizationException('invalid8.manifest')


@istest
def double_open():
"""
Expand Down Expand Up @@ -188,6 +199,21 @@ def primary_attr_block_series():
assert tokens[7].ty == tokenize.Token.types.TEMPLATE


@istest
def primary_attr_block_empty_body():
"""
Unit: Tokenizer Primary Attribute Block Empty Body
Verifies that tokenization proceeds correctly when a "Primary Attribute"
style block is given a "{}" body
"""
tokens = tokenize_filename(get_full_path('valid7.manifest'))
assert len(tokens) == 4
assert tokens[0].ty == tokenize.Token.types.IDENTIFIER
assert tokens[1].ty == tokenize.Token.types.TEMPLATE
assert tokens[2].ty == tokenize.Token.types.BLOCK_START
assert tokens[3].ty == tokenize.Token.types.BLOCK_END


@istest
def token_to_string():
"""
Expand Down

0 comments on commit b702437

Please sign in to comment.