assert_type 0.0.4 → 0.0.5

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.
@@ -8,5 +8,13 @@ module AssertType
8
8
  @children = []
9
9
  end
10
10
 
11
+ def first
12
+ @children.first
13
+ end
14
+
15
+ def self.root
16
+ new "_root"
17
+ end
18
+
11
19
  end
12
20
  end
@@ -14,11 +14,9 @@ module AssertType
14
14
  end
15
15
 
16
16
  def parse
17
- first_token = tokens.shift
18
- main_type = first_token.value
19
- node = TypeNode.new main_type
20
- previous_word = node
21
- current_words = []
17
+ root = TypeNode.root
18
+ previous_word = root
19
+ current_words = [root]
22
20
  tokens.each do |token|
23
21
  case token.name
24
22
  when :open_angle
@@ -31,7 +29,7 @@ module AssertType
31
29
  # ignore commas
32
30
  end
33
31
  end
34
- node
32
+ root
35
33
  end
36
34
 
37
35
  def tokens
@@ -2,15 +2,35 @@ module AssertType
2
2
  class TypeValidator
3
3
  class << self
4
4
 
5
- # @param type_node [AssertType::TypeNode]
5
+ # @param type_node [AssertType::TypeNode, Array<AssertType::TypeNode>]
6
6
  # @param value [Object]
7
7
  def valid? type_node, value
8
- type_node.name == value.class.to_s &&
9
- children_valid?(type_node, value)
8
+ if type_node.name == "_root"
9
+ type_node.children.any? do |node|
10
+ valid? node, value
11
+ end
12
+ else
13
+ type_matches?(type_node.name, value) &&
14
+ children_valid?(type_node, value)
15
+ end
10
16
  end
11
17
 
12
18
  private
13
19
 
20
+ def type_matches? type_name, value
21
+ case type_name
22
+ when "nil"
23
+ value == nil
24
+ when "true"
25
+ value == true
26
+ when "false"
27
+ value == false
28
+ else
29
+ type_name == value.class.to_s
30
+ end
31
+
32
+ end
33
+
14
34
  def children_valid? type_node, value
15
35
  type_node.children.empty? ||
16
36
  value_empty?(value) ||
@@ -1,3 +1,3 @@
1
1
  module AssertType
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -19,6 +19,16 @@ describe AssertType do
19
19
  expect { at_assert_type [Fixnum, String], 1.1 }.to raise_error(AssertType::AssertionError) { |error|
20
20
  error.message.should include %{expected Fixnum or String but was 1.1}
21
21
  }
22
+ at_assert_type "Array<Fixnum>, nil", [1,2,3]
23
+ at_assert_type "Array<Fixnum>, nil", []
24
+ at_assert_type "Array<Fixnum>, nil", nil
25
+ at_assert_type "String, Symbol", "hello"
26
+ expect { at_assert_type "Array<Fixnum>, nil", false }.to raise_error(AssertType::AssertionError)
27
+ expect { at_assert_type "Array<Fixnum>, nil", [nil] }.to raise_error(AssertType::AssertionError)
28
+
29
+ at_assert_type "String, Symbol", "hello"
30
+ at_assert_type "String, Symbol", :Symbol
31
+ expect { at_assert_type "String, Symbol", 42 }.to raise_error(AssertType::AssertionError)
22
32
 
23
33
  end
24
34
 
@@ -7,36 +7,46 @@ module AssertType
7
7
  context "converts string to node tree" do
8
8
 
9
9
  it "example: Array" do
10
- node = TypeStringParser.parse("Array")
11
- node.name.should == "Array"
12
- node.children.length.should == 0
10
+ root = TypeStringParser.parse("Array")
11
+ root.first.name.should == "Array"
12
+ root.first.children.length.should == 0
13
13
  end
14
14
 
15
15
  it "example: Array<Fixnum>" do
16
- node = TypeStringParser.parse("Array<Fixnum>")
17
- node.name.should == "Array"
18
- node.children.length.should == 1
19
- node.children.first.name.should == "Fixnum"
16
+ root = TypeStringParser.parse("Array<Fixnum>")
17
+ root.first.name.should == "Array"
18
+ root.first.children.length.should == 1
19
+ root.first.children.first.name.should == "Fixnum"
20
20
  end
21
21
 
22
22
  it "example: Array<Fixnum, String>" do
23
- node = TypeStringParser.parse("Array<Fixnum, String>")
24
- node.name.should == "Array"
25
- node.children.length.should == 2
26
- node.children[0].name.should == "Fixnum"
27
- node.children[1].name.should == "String"
23
+ root = TypeStringParser.parse("Array<Fixnum, String>")
24
+ root.first.name.should == "Array"
25
+ root.first.children.length.should == 2
26
+ root.first.children[0].name.should == "Fixnum"
27
+ root.first.children[1].name.should == "String"
28
28
  end
29
29
 
30
30
  it "example: Array<Array<Fixnum>, Array<String>>" do
31
- node = TypeStringParser.parse("Array<Array<Fixnum>, Array<String>>")
32
- node.name.should == "Array"
33
- node.children.length.should == 2
34
- node.children[0].name.should == "Array"
35
- node.children[1].name.should == "Array"
36
- node.children[0].children.length.should == 1
37
- node.children[0].children[0].name.should == "Fixnum"
38
- node.children[1].children.length.should == 1
39
- node.children[1].children[0].name.should == "String"
31
+ root = TypeStringParser.parse("Array<Array<Fixnum>, Array<String>>")
32
+ root.first.name.should == "Array"
33
+ root.first.children.length.should == 2
34
+ root.first.children[0].name.should == "Array"
35
+ root.first.children[1].name.should == "Array"
36
+ root.first.children[0].children.length.should == 1
37
+ root.first.children[0].children[0].name.should == "Fixnum"
38
+ root.first.children[1].children.length.should == 1
39
+ root.first.children[1].children[0].name.should == "String"
40
+ end
41
+
42
+ it "example: Array<Fixnum>, nil" do
43
+ root = TypeStringParser.parse("Array<Fixnum>, nil")
44
+ root.children.length.should == 2
45
+ root.children[0].name.should == "Array"
46
+ root.children[0].children.length.should == 1
47
+ root.children[0].children.first.name.should == "Fixnum"
48
+ root.children[1].name.should == "nil"
49
+ root.children[1].children.length.should == 0
40
50
  end
41
51
 
42
52
  end
@@ -14,6 +14,10 @@ module AssertType
14
14
  [:word, "Array"], [:open_angle, "<"], [:word, "Fixnum"], [:comma, ","], [:word, "String"], [:close_angle, ">"]
15
15
  ]
16
16
 
17
+ TypeStringTokeniser.tokenise("Array<Fixnum>, nil").collect{|t|[t.name,t.value]}.should == [
18
+ [:word, "Array"], [:open_angle, "<"], [:word, "Fixnum"], [:close_angle, ">"], [:comma, ","], [:word, "nil"]
19
+ ]
20
+
17
21
  end
18
22
 
19
23
  end
@@ -5,7 +5,9 @@ module AssertType
5
5
  describe TypeValidator do
6
6
 
7
7
  it "example: Array" do
8
- node = stub("TypeNode", :name => "Array", :children => [])
8
+ node = stub("TypeNode", :name => "_root", :children => [
9
+ stub("TypeNode", :name => "Array", :children => [])
10
+ ])
9
11
  TypeValidator.valid?(node, []).should be_true
10
12
  TypeValidator.valid?(node, [1,2,3]).should be_true
11
13
  TypeValidator.valid?(node, ["one","two","three"]).should be_true
@@ -13,7 +15,9 @@ module AssertType
13
15
  end
14
16
 
15
17
  it "example: Set" do
16
- node = stub("TypeNode", :name => "Set", :children => [])
18
+ node = stub("TypeNode", :name => "_root", :children => [
19
+ stub("TypeNode", :name => "Set", :children => [])
20
+ ])
17
21
  TypeValidator.valid?(node, Set.new).should be_true
18
22
  TypeValidator.valid?(node, Set.new([1,2,3])).should be_true
19
23
  TypeValidator.valid?(node, Set.new(["one","two","three"])).should be_true
@@ -24,7 +28,9 @@ module AssertType
24
28
  end
25
29
 
26
30
  it "example: Hash" do
27
- node = stub("TypeNode", :name => "Hash", :children => [])
31
+ node = stub("TypeNode", :name => "_root", :children => [
32
+ stub("TypeNode", :name => "Hash", :children => [])
33
+ ])
28
34
  TypeValidator.valid?(node, {}).should be_true
29
35
  TypeValidator.valid?(node, {:one => 1, :two => 2}).should be_true
30
36
  TypeValidator.valid?(node, []).should be_false
@@ -33,8 +39,10 @@ module AssertType
33
39
  end
34
40
 
35
41
  it "example: Array<Fixnum>" do
36
- node = stub("TypeNode", :name => "Array", :children => [
37
- stub("TypeNode", :name => "Fixnum", :children => [])
42
+ node = stub("TypeNode", :name => "_root", :children => [
43
+ stub("TypeNode", :name => "Array", :children => [
44
+ stub("TypeNode", :name => "Fixnum", :children => [])
45
+ ])
38
46
  ])
39
47
  TypeValidator.valid?(node, [1,2,3]).should be_true
40
48
  TypeValidator.valid?(node, []).should be_true
@@ -42,9 +50,11 @@ module AssertType
42
50
  end
43
51
 
44
52
  it "example: Array<Fixnum, String>" do
45
- node = stub("TypeNode", :name => "Array", :children => [
46
- stub("TypeNode", :name => "Fixnum", :children => []),
47
- stub("TypeNode", :name => "String", :children => [])
53
+ node = stub("TypeNode", :name => "_root", :children => [
54
+ stub("TypeNode", :name => "Array", :children => [
55
+ stub("TypeNode", :name => "Fixnum", :children => []),
56
+ stub("TypeNode", :name => "String", :children => [])
57
+ ])
48
58
  ])
49
59
  TypeValidator.valid?(node, [1,2,3]).should be_true
50
60
  TypeValidator.valid?(node, ["one", "two", "three"]).should be_true
@@ -53,9 +63,11 @@ module AssertType
53
63
  end
54
64
 
55
65
  it "example: Array<Array<Fixnum>>" do
56
- node = stub("TypeNode", :name => "Array", :children => [
66
+ node = stub("TypeNode", :name => "_root", :children => [
57
67
  stub("TypeNode", :name => "Array", :children => [
58
- stub("TypeNode", :name => "Fixnum", :children => [])
68
+ stub("TypeNode", :name => "Array", :children => [
69
+ stub("TypeNode", :name => "Fixnum", :children => [])
70
+ ])
59
71
  ])
60
72
  ])
61
73
  TypeValidator.valid?(node, [[1,2,3]]).should be_true
@@ -68,9 +80,11 @@ module AssertType
68
80
  end
69
81
 
70
82
  it "example: Array<Set<Fixnum>>" do
71
- node = stub("TypeNode", :name => "Array", :children => [
72
- stub("TypeNode", :name => "Set", :children => [
73
- stub("TypeNode", :name => "Fixnum", :children => []),
83
+ node = stub("TypeNode", :name => "_root", :children => [
84
+ stub("TypeNode", :name => "Array", :children => [
85
+ stub("TypeNode", :name => "Set", :children => [
86
+ stub("TypeNode", :name => "Fixnum", :children => []),
87
+ ])
74
88
  ])
75
89
  ])
76
90
 
@@ -82,6 +96,19 @@ module AssertType
82
96
  TypeValidator.valid?(node, [[]]).should be_false
83
97
  end
84
98
 
99
+ it "example: Array<Fixnum>, nil" do
100
+ node = stub("TypeNode", :name => "_root", :children => [
101
+ stub("TypeNode", :name => "Array", :children => [
102
+ stub("TypeNode", :name => "Fixnum", :children => []),
103
+ ]),
104
+ stub("TypeNode", :name => "nil", :children => [])
105
+ ])
106
+ TypeValidator.valid?(node, []).should be_true
107
+ TypeValidator.valid?(node, [1,2,3]).should be_true
108
+ TypeValidator.valid?(node, nil).should be_true
109
+ TypeValidator.valid?(node, false).should be_false
110
+ end
111
+
85
112
  ## Note: we can't parse this yet
86
113
  #xit "example: Hash{Symbol => Fixnum}" do
87
114
  # node = stub("TypeNode", :name => "Hash", :children => [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert_type
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joel Plane