Example #1
0
            if isinstance(val, Command):
                result.append(val)
            else:
                result.extend(val)
        return result


class has_a_space(validators.Validator):
    def validate(self, meta, val):
        if ' ' not in val:
            raise BadOption(
                "Expected string to have a space (<ACTION> <COMMAND>)",
                meta=meta,
                got=val)
        return val


string_command_spec = lambda: sb.container_spec(
    Command, sb.valid_string_spec(has_a_space()))

# Only support ADD commands for the dictionary representation atm
dict_key = sb.valid_string_spec(validators.choice("ADD"))
dictionary_command_spec = lambda: convert_dict_command_spec(
    sb.dictof(dict_key, complex_ADD_spec()))

# The main spec
# We match against, strings, lists, dictionaries and Command objects with different specs
command_spec = lambda: sb.match_spec(
    (six.string_types, string_command_spec()), (list, array_command_spec()),
    (dict, dictionary_command_spec()), (Command, sb.any_spec()))
    it "complains if the key is in the value":
        key = mock.Mock(name="key")
        reason = mock.Mock(name="reason")
        with self.fuzzyAssertRaisesError(DeprecatedKey, key=key, reason=reason):
            va.deprecated_key(key, reason).normalise(self.meta, {key: 1})

    it "doesn't complain if the key is not in the value":
        key = mock.Mock(name="key")
        reason = mock.Mock(name="reason")
        va.deprecated_key(key, reason).normalise(self.meta, {})
        assert True

    it "doesn't fail if the val is not iterable":
        key = mock.Mock(name="key")
        reason = mock.Mock(name="reason")
        va.deprecated_key(key, reason).normalise(self.meta, None)
        assert True

describe TestCase, "choice":
    before_each:
        self.meta = mock.Mock(name="meta")

    it "complains if the val is not one of the choices":
        with self.fuzzyAssertRaisesError(BadSpecValue, "Expected the value to be one of the valid choices", got=4, choices=(1, 2, 3), meta=self.meta):
            va.choice(1, 2, 3).normalise(self.meta, 4)

    it "returns the val if it's one of the choices":
        self.assertIs(va.choice(1, 2, 3, 4).normalise(self.meta, 4), 4)

    it "complains if the key is in the value":
        key = mock.Mock(name="key")
        reason = mock.Mock(name="reason")
        with self.fuzzyAssertRaisesError(DeprecatedKey, key=key, reason=reason):
            va.deprecated_key(key, reason).normalise(self.meta, {key: 1})

    it "doesn't complain if the key is not in the value":
        key = mock.Mock(name="key")
        reason = mock.Mock(name="reason")
        va.deprecated_key(key, reason).normalise(self.meta, {})
        assert True

    it "doesn't fail if the val is not iterable":
        key = mock.Mock(name="key")
        reason = mock.Mock(name="reason")
        va.deprecated_key(key, reason).normalise(self.meta, None)
        assert True

describe TestCase, "choice":
    before_each:
        self.meta = mock.Mock(name="meta")

    it "complains if the val is not one of the choices":
        with self.fuzzyAssertRaisesError(BadSpecValue, "Expected the value to be one of the valid choices", got=4, choices=(1, 2, 3), meta=self.meta):
            va.choice(1, 2, 3).normalise(self.meta, 4)

    it "returns the val if it's one of the choices":
        self.assertIs(va.choice(1, 2, 3, 4).normalise(self.meta, 4), 4)

Example #4
0
    def normalise(self, meta, val):
        result = []
        for val in self.spec.normalise(meta, val).values():
            if isinstance(val, Command):
                result.append(val)
            else:
                result.extend(val)
        return result

class has_a_space(validators.Validator):
    def validate(self, meta, val):
        if ' ' not in val:
            raise BadOption("Expected string to have a space (<ACTION> <COMMAND>)", meta=meta, got=val)
        return val

string_command_spec = lambda: sb.container_spec(Command, sb.valid_string_spec(has_a_space()))

# Only support ADD commands for the dictionary representation atm
dict_key = sb.valid_string_spec(validators.choice("ADD"))
dictionary_command_spec = lambda: convert_dict_command_spec(sb.dictof(dict_key, complex_ADD_spec()))

# The main spec
# We match against, strings, lists, dictionaries and Command objects with different specs
command_spec = lambda: sb.match_spec(
      (six.string_types, string_command_spec())
    , (list, array_command_spec())
    , (dict, dictionary_command_spec())
    , (Command, sb.any_spec())
    )