duck-hunt 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +526 -0
  3. data/Rakefile +15 -0
  4. data/lib/duck-hunt.rb +17 -0
  5. data/lib/duck-hunt/hash_helpers.rb +28 -0
  6. data/lib/duck-hunt/properties.rb +13 -0
  7. data/lib/duck-hunt/properties/array.rb +81 -0
  8. data/lib/duck-hunt/properties/boolean.rb +10 -0
  9. data/lib/duck-hunt/properties/float.rb +10 -0
  10. data/lib/duck-hunt/properties/integer.rb +9 -0
  11. data/lib/duck-hunt/properties/nested_hash.rb +61 -0
  12. data/lib/duck-hunt/properties/nil.rb +15 -0
  13. data/lib/duck-hunt/properties/property.rb +85 -0
  14. data/lib/duck-hunt/properties/string.rb +10 -0
  15. data/lib/duck-hunt/properties/validator_lookup.rb +27 -0
  16. data/lib/duck-hunt/schemas.rb +8 -0
  17. data/lib/duck-hunt/schemas/array_schema.rb +254 -0
  18. data/lib/duck-hunt/schemas/hash_schema.rb +135 -0
  19. data/lib/duck-hunt/schemas/property_lookup.rb +32 -0
  20. data/lib/duck-hunt/schemas/schema_definition.rb +25 -0
  21. data/lib/duck-hunt/string_helpers.rb +25 -0
  22. data/lib/duck-hunt/validators.rb +16 -0
  23. data/lib/duck-hunt/validators/accepted_values.rb +19 -0
  24. data/lib/duck-hunt/validators/divisible_by.rb +19 -0
  25. data/lib/duck-hunt/validators/equal_to.rb +19 -0
  26. data/lib/duck-hunt/validators/greater_than.rb +19 -0
  27. data/lib/duck-hunt/validators/greater_than_or_equal_to.rb +19 -0
  28. data/lib/duck-hunt/validators/less_than.rb +19 -0
  29. data/lib/duck-hunt/validators/less_than_or_equal_to.rb +19 -0
  30. data/lib/duck-hunt/validators/matches.rb +18 -0
  31. data/lib/duck-hunt/validators/not_divisible_by.rb +19 -0
  32. data/lib/duck-hunt/validators/not_equal_to.rb +19 -0
  33. data/lib/duck-hunt/validators/rejected_values.rb +19 -0
  34. data/lib/duck-hunt/validators/validator.rb +16 -0
  35. data/lib/duck-hunt/version.rb +3 -0
  36. data/test/properties/array_test.rb +837 -0
  37. data/test/properties/boolean_test.rb +37 -0
  38. data/test/properties/float_test.rb +49 -0
  39. data/test/properties/integer_test.rb +48 -0
  40. data/test/properties/nested_hash_test.rb +465 -0
  41. data/test/properties/nil_test.rb +30 -0
  42. data/test/properties/property_test.rb +193 -0
  43. data/test/properties/string_test.rb +24 -0
  44. data/test/properties/validator_lookup_test.rb +25 -0
  45. data/test/schemas/array_schema_test.rb +797 -0
  46. data/test/schemas/hash_schema_test.rb +264 -0
  47. data/test/schemas/property_lookup_test.rb +41 -0
  48. data/test/schemas/schema_definition_test.rb +51 -0
  49. data/test/test_helper.rb +29 -0
  50. data/test/test_helper/test_classes.rb +74 -0
  51. data/test/validators/accepted_values_test.rb +46 -0
  52. data/test/validators/divisible_by_test.rb +38 -0
  53. data/test/validators/equal_to_test.rb +38 -0
  54. data/test/validators/greater_than_or_equal_to_test.rb +39 -0
  55. data/test/validators/greater_than_test.rb +39 -0
  56. data/test/validators/less_than_or_equal_to_test.rb +40 -0
  57. data/test/validators/less_than_test.rb +39 -0
  58. data/test/validators/matches_test.rb +43 -0
  59. data/test/validators/not_divisible_by_test.rb +38 -0
  60. data/test/validators/not_equal_to_test.rb +38 -0
  61. data/test/validators/rejected_values_test.rb +46 -0
  62. data/test/validators/validator_test.rb +23 -0
  63. metadata +196 -0
@@ -0,0 +1,46 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::AcceptedValues, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::AcceptedValues.new([1,2,3])
6
+ validator.values.must_equal [1,2,3]
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::AcceptedValues.new
12
+ }.must_raise ArgumentError
13
+ end
14
+
15
+ it "should raise an exception if the value provided is not a hash" do
16
+ lambda{
17
+ DuckHunt::Validators::AcceptedValues.new(3)
18
+ }.must_raise ArgumentError
19
+ end
20
+ end
21
+
22
+ describe DuckHunt::Validators::AcceptedValues, "Validation" do
23
+ before do
24
+ @validator = DuckHunt::Validators::AcceptedValues.new([1,2,3])
25
+ end
26
+
27
+ it "returns true if the value provided is one of the accepted values" do
28
+ @validator.valid?(1).must_equal true
29
+ @validator.valid?(2).must_equal true
30
+ @validator.valid?(3).must_equal true
31
+ end
32
+
33
+ it "returns false if the value provided is not one of the accepted values" do
34
+ @validator.valid?(4).must_equal false
35
+ @validator.valid?(0).must_equal false
36
+ end
37
+ end
38
+
39
+
40
+
41
+ describe DuckHunt::Validators::AcceptedValues, "error message" do
42
+ it "should have the correct error message based on the value provided" do
43
+ validator = DuckHunt::Validators::AcceptedValues.new([1,2,3])
44
+ validator.error_message.must_equal "not an accepted value"
45
+ end
46
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::DivisibleBy, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::DivisibleBy.new(3)
6
+ validator.value.must_equal 3
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::DivisibleBy.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::DivisibleBy, "Validation" do
17
+ before do
18
+ @validator = DuckHunt::Validators::DivisibleBy.new(3)
19
+ end
20
+
21
+ it "returns true if the value provided is divisible by the value provided" do
22
+ @validator.valid?(3).must_equal true
23
+ @validator.valid?(9).must_equal true
24
+ end
25
+
26
+ it "returns false if the value provided is not divisible by to the value given" do
27
+ @validator.valid?(5).must_equal false
28
+ end
29
+ end
30
+
31
+
32
+
33
+ describe DuckHunt::Validators::DivisibleBy, "error message" do
34
+ it "should have the correct error message based on the value provided" do
35
+ validator = DuckHunt::Validators::DivisibleBy.new(3)
36
+ validator.error_message.must_equal "not divisible by `3`"
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::EqualTo, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::EqualTo.new(3)
6
+ validator.value.must_equal 3
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::EqualTo.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::EqualTo, "Validation" do
17
+ before do
18
+ @validator = DuckHunt::Validators::EqualTo.new(3)
19
+ end
20
+
21
+ it "returns true if the value provided is equal to the value given" do
22
+ @validator.valid?(3).must_equal true
23
+ end
24
+
25
+ it "returns false if the value provided is not equal to the value provided" do
26
+ @validator.valid?(4).must_equal false
27
+ @validator.valid?(0).must_equal false
28
+ end
29
+ end
30
+
31
+
32
+
33
+ describe DuckHunt::Validators::EqualTo, "error message" do
34
+ it "should have the correct error message based on the value provided" do
35
+ validator = DuckHunt::Validators::EqualTo.new(3)
36
+ validator.error_message.must_equal "not equal to `3`"
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::GreaterThanOrEqualTo, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::GreaterThanOrEqualTo.new(3)
6
+ validator.value.must_equal 3
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::GreaterThanOrEqualTo.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::GreaterThanOrEqualTo, "Validation" do
17
+ before do
18
+ @validator = DuckHunt::Validators::GreaterThanOrEqualTo.new(3)
19
+ end
20
+
21
+ it "returns true if the value provided is greater than or equal to the value given" do
22
+ @validator.valid?(4).must_equal true
23
+ @validator.valid?(41).must_equal true
24
+ @validator.valid?(3).must_equal true
25
+ end
26
+
27
+ it "returns false if the value provided is less than the value given" do
28
+ @validator.valid?(0).must_equal false
29
+ end
30
+ end
31
+
32
+
33
+
34
+ describe DuckHunt::Validators::GreaterThanOrEqualTo, "error message" do
35
+ it "should have the correct error message based on the value provided" do
36
+ validator = DuckHunt::Validators::GreaterThanOrEqualTo.new(3)
37
+ validator.error_message.must_equal "less than `3`"
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::GreaterThan, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::GreaterThan.new(3)
6
+ validator.value.must_equal 3
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::GreaterThan.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::GreaterThan, "Validation" do
17
+ before do
18
+ @validator = DuckHunt::Validators::GreaterThan.new(3)
19
+ end
20
+
21
+ it "returns true if the value provided is greater than the value given" do
22
+ @validator.valid?(4).must_equal true
23
+ @validator.valid?(41).must_equal true
24
+ end
25
+
26
+ it "returns false if the value provided is less than the value provided" do
27
+ @validator.valid?(3).must_equal false
28
+ @validator.valid?(0).must_equal false
29
+ end
30
+ end
31
+
32
+
33
+
34
+ describe DuckHunt::Validators::GreaterThan, "error message" do
35
+ it "should have the correct error message based on the value provided" do
36
+ validator = DuckHunt::Validators::GreaterThan.new(3)
37
+ validator.error_message.must_equal "less than `3`"
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::LessThanOrEqualTo, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::LessThanOrEqualTo.new(3)
6
+ validator.value.must_equal 3
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::LessThanOrEqualTo.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::LessThanOrEqualTo, "Validation" do
17
+ before do
18
+ @validator = DuckHunt::Validators::LessThanOrEqualTo.new(3)
19
+ end
20
+
21
+ it "returns true if the value provided is less than or equal to the value given" do
22
+ @validator.valid?(3).must_equal true
23
+ @validator.valid?(2).must_equal true
24
+ @validator.valid?(0).must_equal true
25
+ end
26
+
27
+ it "returns false if the value provided is greater than the value provided" do
28
+ @validator.valid?(4).must_equal false
29
+ @validator.valid?(5).must_equal false
30
+ end
31
+ end
32
+
33
+
34
+
35
+ describe DuckHunt::Validators::LessThanOrEqualTo, "error message" do
36
+ it "should have the correct error message based on the value provided" do
37
+ validator = DuckHunt::Validators::LessThanOrEqualTo.new(3)
38
+ validator.error_message.must_equal "greater than `3`"
39
+ end
40
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::LessThan, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::LessThan.new(3)
6
+ validator.value.must_equal 3
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::LessThan.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::LessThan, "Validation" do
17
+ before do
18
+ @validator = DuckHunt::Validators::LessThan.new(3)
19
+ end
20
+
21
+ it "returns true if the value provided is less than the value given" do
22
+ @validator.valid?(2).must_equal true
23
+ @validator.valid?(0).must_equal true
24
+ end
25
+
26
+ it "returns false if the value provided is greater than the value provided" do
27
+ @validator.valid?(3).must_equal false
28
+ @validator.valid?(5).must_equal false
29
+ end
30
+ end
31
+
32
+
33
+
34
+ describe DuckHunt::Validators::LessThan, "error message" do
35
+ it "should have the correct error message based on the value provided" do
36
+ validator = DuckHunt::Validators::LessThan.new(3)
37
+ validator.error_message.must_equal "greater than `3`"
38
+ end
39
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::Matches, "initialization" do
4
+ it "should create an instance with the provided regex" do
5
+ validator = DuckHunt::Validators::Matches.new(/\d{1,2}/)
6
+ validator.regex.must_equal /\d{1,2}/
7
+ end
8
+
9
+ it "should raise an exception if a Regex is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::Matches.new
12
+ }.must_raise TypeError
13
+ end
14
+
15
+ it "should accept a string as a valid Regexp" do
16
+ validator = DuckHunt::Validators::Matches.new('\d{1,2}')
17
+ validator.regex.must_equal /\d{1,2}/
18
+ end
19
+ end
20
+
21
+ describe DuckHunt::Validators::Matches, "Validation" do
22
+ before do
23
+ @validator = DuckHunt::Validators::Matches.new(/\d{1,2}/)
24
+ end
25
+
26
+ it "returns true if the value provided matches the regexp" do
27
+ @validator.valid?("1").must_equal true
28
+ @validator.valid?("12").must_equal true
29
+ end
30
+
31
+ it "returns false if the value provided does not match the regexp" do
32
+ @validator.valid?("hello").must_equal false
33
+ end
34
+ end
35
+
36
+
37
+
38
+ describe DuckHunt::Validators::Matches, "error message" do
39
+ it "should have the correct error message" do
40
+ validator = DuckHunt::Validators::Matches.new(/\d{1,2}/)
41
+ validator.error_message.must_equal "No matches for Regexp"
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::NotDivisibleBy, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::NotDivisibleBy.new(3)
6
+ validator.value.must_equal 3
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::NotDivisibleBy.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::NotDivisibleBy, "Validation" do
17
+ before do
18
+ @validator = DuckHunt::Validators::NotDivisibleBy.new(3)
19
+ end
20
+
21
+ it "returns true if the value provided is not divisible by the value provided" do
22
+ @validator.valid?(5).must_equal true
23
+ end
24
+
25
+ it "returns false if the value provided is divisible by to the value given" do
26
+ @validator.valid?(3).must_equal false
27
+ @validator.valid?(9).must_equal false
28
+ end
29
+ end
30
+
31
+
32
+
33
+ describe DuckHunt::Validators::NotDivisibleBy, "error message" do
34
+ it "should have the correct error message based on the value provided" do
35
+ validator = DuckHunt::Validators::NotDivisibleBy.new(3)
36
+ validator.error_message.must_equal "divisible by `3`"
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::NotEqualTo, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::NotEqualTo.new(3)
6
+ validator.value.must_equal 3
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::NotEqualTo.new
12
+ }.must_raise ArgumentError
13
+ end
14
+ end
15
+
16
+ describe DuckHunt::Validators::NotEqualTo, "Validation" do
17
+ before do
18
+ @validator = DuckHunt::Validators::NotEqualTo.new(3)
19
+ end
20
+
21
+ it "returns true if the value provided is not equal to the value provided" do
22
+ @validator.valid?(4).must_equal true
23
+ @validator.valid?(0).must_equal true
24
+ end
25
+
26
+ it "returns false if the value provided is equal to the value given" do
27
+ @validator.valid?(3).must_equal false
28
+ end
29
+ end
30
+
31
+
32
+
33
+ describe DuckHunt::Validators::NotEqualTo, "error message" do
34
+ it "should have the correct error message based on the value provided" do
35
+ validator = DuckHunt::Validators::NotEqualTo.new(3)
36
+ validator.error_message.must_equal "equal to `3`"
37
+ end
38
+ end
@@ -0,0 +1,46 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe DuckHunt::Validators::RejectedValues, "initialization" do
4
+ it "should create an instance with the provided value" do
5
+ validator = DuckHunt::Validators::RejectedValues.new([1,2,3])
6
+ validator.values.must_equal [1,2,3]
7
+ end
8
+
9
+ it "should raise an exception if a value is not provided" do
10
+ lambda{
11
+ DuckHunt::Validators::RejectedValues.new
12
+ }.must_raise ArgumentError
13
+ end
14
+
15
+ it "should raise an exception if the value provided is not a hash" do
16
+ lambda{
17
+ DuckHunt::Validators::RejectedValues.new(3)
18
+ }.must_raise ArgumentError
19
+ end
20
+ end
21
+
22
+ describe DuckHunt::Validators::RejectedValues, "Validation" do
23
+ before do
24
+ @validator = DuckHunt::Validators::RejectedValues.new([1,2,3])
25
+ end
26
+
27
+ it "returns true if the value provided is not one of the rejected values" do
28
+ @validator.valid?(4).must_equal true
29
+ @validator.valid?(0).must_equal true
30
+ end
31
+
32
+ it "returns false if the value provided is one of the rejected values" do
33
+ @validator.valid?(1).must_equal false
34
+ @validator.valid?(2).must_equal false
35
+ @validator.valid?(3).must_equal false
36
+ end
37
+ end
38
+
39
+
40
+
41
+ describe DuckHunt::Validators::RejectedValues, "error message" do
42
+ it "should have the correct error message based on the value provided" do
43
+ validator = DuckHunt::Validators::RejectedValues.new([1,2,3])
44
+ validator.error_message.must_equal "a rejected value"
45
+ end
46
+ end