logic 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ require 'truth_table'
5
5
  module LogicStatement
6
6
 
7
7
  def test_cases
8
- @test_cases ||= TestCaseSet.new(condition_count, self)
8
+ @test_cases ||= TestCaseSet.new(condition_identifiers, self)
9
9
  end
10
10
 
11
11
  def truth_table
@@ -16,10 +16,6 @@ module LogicStatement
16
16
  test_cases.mcdc_pairs
17
17
  end
18
18
 
19
- def condition_count
20
- condition_identifiers.count
21
- end
22
-
23
19
  end
24
20
 
25
21
  module Condition
data/lib/test_case_set.rb CHANGED
@@ -2,24 +2,32 @@ require 'test_case'
2
2
 
3
3
  class TestCaseSet
4
4
 
5
- def initialize(condition_count, decision)
6
- @condition_count = condition_count
5
+ def initialize(condition_identifiers, decision)
6
+ @condition_identifiers = condition_identifiers
7
7
  @decision = decision
8
8
  @test_cases = create_cases
9
9
  end
10
10
 
11
11
  def create_cases
12
12
  case_numbers.map do |case_number|
13
- TestCase.new(case_number, @condition_count, @decision)
13
+ TestCase.new(case_number, condition_count, @decision)
14
14
  end
15
15
  end
16
16
 
17
17
  def case_numbers
18
- 1..2**@condition_count
18
+ 1..2**condition_count
19
19
  end
20
20
 
21
- def condition_identifiers
22
- identifier_range.take(@condition_count)
21
+ def condition_count
22
+ @condition_identifiers.count
23
+ end
24
+
25
+ def input_to_condition_mapping
26
+ input_identifiers.zip(@condition_identifiers)
27
+ end
28
+
29
+ def input_identifiers
30
+ identifier_range.take(condition_count)
23
31
  end
24
32
 
25
33
  def identifier_range
@@ -27,7 +35,7 @@ class TestCaseSet
27
35
  end
28
36
 
29
37
  def mcdc_pairs
30
- condition_identifiers.map do |condition_identifier|
38
+ input_identifiers.map do |condition_identifier|
31
39
  "#{condition_identifier} => #{mcdc_pairs_for(condition_identifier)}"
32
40
  end
33
41
  end
@@ -44,12 +52,16 @@ class TestCaseSet
44
52
  identifier_range.to_a.index(condition_identifier)
45
53
  end
46
54
 
47
- def count
48
- @test_cases.count
55
+ def formatted_input_to_condition_maping(format)
56
+ input_to_condition_mapping.inject("") do |output, input_condition_pair|
57
+ output += format % input_condition_pair
58
+ end
49
59
  end
50
60
 
51
- def inject(initial, &block)
52
- @test_cases.inject(initial, &block)
61
+ def formatted(format)
62
+ @test_cases.inject("") do |output, test_case|
63
+ output += test_case.formatted(format)
64
+ end
53
65
  end
54
66
 
55
67
  end
data/lib/truth_table.rb CHANGED
@@ -5,17 +5,21 @@ class TruthTable
5
5
  end
6
6
 
7
7
  def to_s
8
- header + formatted_cases + "\n"
8
+ formatted_input_to_condition_maping + "\n" +
9
+ table_header + "\n" +
10
+ formatted_cases + "\n"
9
11
  end
10
12
 
11
- def header
12
- " %s | output\n" % @test_cases.condition_identifiers.join(" ")
13
+ def formatted_input_to_condition_maping
14
+ @test_cases.formatted_input_to_condition_maping("%s <= %s\n")
15
+ end
16
+
17
+ def table_header
18
+ " %s | output" % @test_cases.input_identifiers.join(" ")
13
19
  end
14
20
 
15
21
  def formatted_cases
16
- @test_cases.inject("") do |output, test_case|
17
- output += test_case.formatted("%2d) %s | %3d\n")
18
- end
22
+ @test_cases.formatted("%2d) %s | %3d\n")
19
23
  end
20
24
 
21
25
  end
@@ -3,76 +3,51 @@ require 'treetop'
3
3
  require 'logic_parser'
4
4
  require 'logic_operations'
5
5
 
6
- describe LogicParser do
6
+ describe LogicParser, :parsing do
7
7
 
8
8
  let(:parser) { LogicParser.new }
9
9
 
10
- describe "parsing a single condition" do
11
-
12
- let(:decision) { parser.parse('Hello') }
13
-
10
+ describe "a single condition" do
11
+
14
12
  it "identifies the condition" do
13
+ decision = parser.parse('Hello')
15
14
  decision.condition_identifiers.should == ['Hello']
16
15
  end
17
16
 
18
- it "has 1 condition" do
19
- decision.condition_count.should == 1
20
- end
21
-
22
17
  end
23
18
 
24
- describe "parsing 'A or B'" do
25
-
26
- let(:decision) { parser.parse('A or B') }
19
+ describe "'A or B'" do
27
20
 
28
21
  it "condition identifiers are ['A','B']" do
22
+ decision = parser.parse('A or B')
29
23
  decision.condition_identifiers.should == ['A','B']
30
24
  end
31
25
 
32
- it "has 2 conditions" do
33
- decision.condition_count.should == 2
34
- end
35
-
36
26
  end
37
27
 
38
- describe "parsing 'A or B or C'" do
39
-
40
- let(:decision) { parser.parse('A or B or C') }
28
+ describe "'A or B or C'" do
41
29
 
42
30
  it "condition identifiers are ['A','B','C']" do
43
- decision.condition_identifiers.should == ['A','B', 'C']
44
- end
45
-
46
- it "has 3 conditions" do
47
- decision.condition_count.should == 3
31
+ decision = parser.parse('A or B or C')
32
+ decision.condition_identifiers.should == ['A','B','C']
48
33
  end
49
34
 
50
35
  end
51
36
 
52
- describe "parsing 'A and B'" do
53
-
54
- let(:decision) { parser.parse('A and B') }
37
+ describe "'A and B'" do
55
38
 
56
39
  it "conditions are ['A','B']" do
40
+ decision = parser.parse('A and B')
57
41
  decision.condition_identifiers.should == ['A','B']
58
42
  end
59
43
 
60
- it "has 2 conditions" do
61
- decision.condition_count.should == 2
62
- end
63
-
64
44
  end
65
45
 
66
- describe "parsing 'A and B and C'" do
67
-
68
- let(:decision) { parser.parse('A and B and C') }
46
+ describe "'A and B and C'" do
69
47
 
70
48
  it "condition identifiers are ['A','B','C']" do
71
- decision.condition_identifiers.should == ['A','B', 'C']
72
- end
73
-
74
- it "has 3 conditions" do
75
- decision.condition_count.should == 3
49
+ decision = parser.parse('A and B and C')
50
+ decision.condition_identifiers.should == ['A','B','C']
76
51
  end
77
52
 
78
53
  end
@@ -116,12 +91,11 @@ describe LogicParser do
116
91
  [0, 1, 1] => 0
117
92
  }
118
93
  }.each do |logic, cases|
119
- describe "parsing '#{logic}'" do
120
-
121
- let(:decision) { parser.parse(logic) }
94
+ describe "'#{logic}'" do
122
95
 
123
96
  cases.each do |conditions, result|
124
97
  it "'#{conditions}' evaluates to '#{result}'" do
98
+ decision = parser.parse(logic)
125
99
  decision.evaluate(conditions).should == result
126
100
  end
127
101
  end
@@ -3,21 +3,12 @@ require 'test_case_set'
3
3
  describe TestCaseSet do
4
4
 
5
5
  describe "with 1 condition" do
6
-
7
- let(:test_case_set) { TestCaseSet.new(1, nil) }
8
6
 
9
- describe :condition_dentifiers do
10
-
11
- it "includes 'a'" do
12
- test_case_set.condition_identifiers.should include('a')
13
- end
14
-
7
+ it "input_dentifiers includes 'a'" do
8
+ test_case_set = TestCaseSet.new(['hello'], nil)
9
+ test_case_set.input_identifiers.should include('a')
15
10
  end
16
11
 
17
- it "has 2 test cases" do
18
- test_case_set.count.should == 2
19
- end
20
-
21
12
  end
22
13
 
23
14
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 9
9
- version: 0.0.9
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bryan Ash