Browse Source

Add tests for the core

master
Dylan Baker 5 years ago
parent
commit
27c55464f2
2 changed files with 51 additions and 2 deletions
  1. 10
    2
      lib/chervil/core.rb
  2. 41
    0
      spec/core_spec.rb

+ 10
- 2
lib/chervil/core.rb View File

@@ -34,7 +34,8 @@ module Chervil
34 34
 
35 35
     def self.arity_check(args, expected_count)
36 36
       if args.size != expected_count
37
-        ::Chervil::Error.new("Expected #{expected_count} arguments but received #{args.size}")
37
+        s = expected_count == 1 ? '' : 's'
38
+        Error.new("Expected #{expected_count} argument#{s} but received #{args.size}")
38 39
       else
39 40
         nil
40 41
       end
@@ -52,7 +53,14 @@ module Chervil
52 53
       ">=" => Proc.new { |args| type_check(args, Float) || compare_pairs(args, :>=) },
53 54
       "and" => Proc.new { |args| !(args.include?(false)) },
54 55
       "or" => Proc.new { |args| args.any? { |arg| !!arg == true } },
55
-      "not" => Proc.new { |args| arity_check(args, 1) || args.first == false ? true : false },
56
+      "not" => Proc.new do |args|
57
+        error = arity_check(args, 1)
58
+        if error.nil?
59
+          args.first == false ? true : false
60
+        else
61
+          error
62
+        end
63
+      end,
56 64
     }
57 65
   end
58 66
 end

+ 41
- 0
spec/core_spec.rb View File

@@ -0,0 +1,41 @@
1
+module Chervil
2
+  RSpec.describe Core do
3
+    it 'does arithmetic' do
4
+      expect(Core::CORE['+'].call([1.0, 2.0])).to eq(3.0)
5
+      expect(Core::CORE['-'].call([3.0, 1.0])).to eq(2.0)
6
+      expect(Core::CORE['*'].call([2.0, 3.0])).to eq(6.0)
7
+      expect(Core::CORE['/'].call([6.0, 2.0])).to eq(3.0)
8
+    end
9
+
10
+    it 'compares values' do
11
+      expect(Core::CORE['='].call([1.0, 2.0])).to eq(false)
12
+      expect(Core::CORE['='].call([1.0, 1.0])).to eq(true)
13
+      expect(Core::CORE['<'].call([2.0, 1.0])).to eq(false)
14
+      expect(Core::CORE['<'].call([2.0, 3.0])).to eq(true)
15
+      expect(Core::CORE['>'].call([2.0, 1.0])).to eq(true)
16
+      expect(Core::CORE['>'].call([2.0, 3.0])).to eq(false)
17
+      expect(Core::CORE['<='].call([2.0, 1.0])).to eq(false)
18
+      expect(Core::CORE['<='].call([2.0, 2.0])).to eq(true)
19
+      expect(Core::CORE['>='].call([2.0, 2.0])).to eq(true)
20
+      expect(Core::CORE['>='].call([2.0, 3.0])).to eq(false)
21
+      expect(Core::CORE['and'].call([2.0, 3.0])).to eq(true)
22
+      expect(Core::CORE['and'].call([2.0, 3.0, false])).to eq(false)
23
+      expect(Core::CORE['or'].call([2.0, 3.0, false])).to eq(true)
24
+      expect(Core::CORE['or'].call([false, false])).to eq(false)
25
+      expect(Core::CORE['not'].call([1.0])).to eq(false)
26
+      expect(Core::CORE['not'].call([false])).to eq(true)
27
+    end
28
+
29
+    it 'returns an error if argument has the wrong type' do
30
+      expect(Core::CORE['+'].call([1.0, "hello"])).to eq(
31
+        Error.new("Expected an argument of type number but got string")
32
+      )
33
+    end
34
+
35
+    it 'returns an error if given the wrong number of arguments' do
36
+      expect(Core::CORE['not'].call([1.0, 2.0])).to eq(
37
+        Error.new("Expected 1 argument but received 2")
38
+      )
39
+    end
40
+  end
41
+end

Loading…
Cancel
Save