Skip to content

Commit 5240c0f

Browse files
authored
Merge pull request #4 from skinnyjames/feature/errors
Feature/errors
2 parents 1508f2c + 76247e4 commit 5240c0f

7 files changed

Lines changed: 126 additions & 22 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mruby/
2-
tmp/
2+
tmp/
3+
.vscode/

build_config.rb.lock

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,23 @@ mruby:
55
git_commit: eb398971bfb43c38db3e04528b68ac9a7ce509bc
66
builds:
77
host:
8+
https://github.com/ksss/mruby-file-stat.git:
9+
url: https://github.com/ksss/mruby-file-stat.git
10+
branch: master
11+
commit: 7d0e63a95a38b34c635b708874ec624c7c860507
12+
version: 0.0.0
813
https://github.com/gromnitsky/mruby-dir-glob.git:
914
url: https://github.com/gromnitsky/mruby-dir-glob.git
1015
branch: master
1116
commit: 334c040a2e2c4c2689f8c3440168011f64d57ada
1217
version: 0.0.1
13-
https://github.com/ksss/mruby-file-stat.git:
14-
url: https://github.com/ksss/mruby-file-stat.git
18+
https://github.com/iij/mruby-regexp-pcre.git:
19+
url: https://github.com/iij/mruby-regexp-pcre.git
1520
branch: master
16-
commit: 7d0e63a95a38b34c635b708874ec624c7c860507
21+
commit: 71bd16ba59239f04aefb73bb6d46d5b581f27b1b
1722
version: 0.0.0
1823
https://github.com/iij/mruby-process.git:
1924
url: https://github.com/iij/mruby-process.git
2025
branch: master
2126
commit: 95da206a5764f4e80146979b8e35bd7a9afd6850
2227
version: 0.0.0
23-
https://github.com/WindProphet/mruby-pure-regexp.git:
24-
url: https://github.com/WindProphet/mruby-pure-regexp.git
25-
branch: master
26-
commit: 9cee46e9afeaa18001467a1a25ea6c9d88612287
27-
version: 0.0.0
28-
https://github.com/iij/mruby-regexp-pcre.git:
29-
url: https://github.com/iij/mruby-regexp-pcre.git
30-
branch: master
31-
commit: 71bd16ba59239f04aefb73bb6d46d5b581f27b1b
32-
version: 0.0.0

lib/matchers.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module Matchers
22
class Expect
3+
attr_reader :actual
4+
35
def initialize(actual)
46
@actual = actual
57
end
68

7-
def to(matcher, error = "Assertion failed for #{matcher.class}")
9+
def to(matcher, error = matcher.error(actual))
810
raise StandardError.new(error) unless matcher.match(@actual)
911
end
1012

@@ -41,17 +43,44 @@ class Be < MatchBase
4143
def match(actual)
4244
@expected.object_id == actual.object_id
4345
end
46+
47+
def error(actual)
48+
"#{actual.class}.object_id(#{actual.object_id}) does not equal #{@expected.class}.object_id(#{@expected.object_id})"
49+
end
50+
end
51+
52+
class Match < MatchBase
53+
def match(actual)
54+
@expected =~ actual
55+
end
56+
57+
def error(actual)
58+
"#{actual.inspect} does not match #{@expected.inspect}"
59+
end
4460
end
4561

4662
class Eql < MatchBase
4763
def match(actual)
4864
@expected == actual
4965
end
66+
67+
def error(actual)
68+
"#{actual.inspect} does not equal #{@expected.inspect}"
69+
end
5070
end
5171

5272
class Include < MatchBase
5373
def match(actual)
54-
actual.include?(@expected)
74+
case @expected
75+
when Array
76+
(@expected - actual).size.zero?
77+
else
78+
actual.include?(@expected)
79+
end
80+
end
81+
82+
def error(actual)
83+
"#{actual.inspect} does not include #{@expected.inspect}"
5584
end
5685
end
5786

@@ -93,6 +122,10 @@ def expect(actual = nil, &block)
93122
end
94123
end
95124

125+
def match(expected)
126+
Match.new(expected)
127+
end
128+
96129
def eql(expected)
97130
Eql.new(expected)
98131
end

mrblib/theorem.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,13 @@ def report_failures(tests)
888888
end
889889
module Matchers
890890
class Expect
891+
attr_reader :actual
892+
891893
def initialize(actual)
892894
@actual = actual
893895
end
894896

895-
def to(matcher, error = "Assertion failed for #{matcher.class}")
897+
def to(matcher, error = matcher.error(actual))
896898
raise StandardError.new(error) unless matcher.match(@actual)
897899
end
898900

@@ -929,17 +931,44 @@ class Be < MatchBase
929931
def match(actual)
930932
@expected.object_id == actual.object_id
931933
end
934+
935+
def error(actual)
936+
"#{actual.class}.object_id(#{actual.object_id}) does not equal #{@expected.class}.object_id(#{@expected.object_id})"
937+
end
938+
end
939+
940+
class Match < MatchBase
941+
def match(actual)
942+
@expected =~ actual
943+
end
944+
945+
def error(actual)
946+
"#{actual.inspect} does not match #{@expected.inspect}"
947+
end
932948
end
933949

934950
class Eql < MatchBase
935951
def match(actual)
936952
@expected == actual
937953
end
954+
955+
def error(actual)
956+
"#{actual.inspect} does not equal #{@expected.inspect}"
957+
end
938958
end
939959

940960
class Include < MatchBase
941961
def match(actual)
942-
actual.include?(@expected)
962+
case @expected
963+
when Array
964+
(@expected - actual).size.zero?
965+
else
966+
actual.include?(@expected)
967+
end
968+
end
969+
970+
def error(actual)
971+
"#{actual.inspect} does not include #{@expected.inspect}"
943972
end
944973
end
945974

@@ -981,6 +1010,10 @@ def expect(actual = nil, &block)
9811010
end
9821011
end
9831012

1013+
def match(expected)
1014+
Match.new(expected)
1015+
end
1016+
9841017
def eql(expected)
9851018
Eql.new(expected)
9861019
end

mruby-bin-theorem.gem

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: mruby-bin-theorem
22
description: A modern test library & runner toolkit for mruby
3-
author: Zero Stars
4-
website:https://github.com/skinnyjames/mruby-bin-theorem
3+
author: skinnyjames
4+
website: https://github.com/skinnyjames/mruby-bin-theorem
55
protocol: git
6-
repository: https://github.com/skinnyjames/mruby-bin-theorem
7-
license: MIT
6+
repository: https://github.com/skinnyjames/mruby-bin-theorem.git
7+
license: MIT
8+
branch: main

src/mrb_theorem.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11

22
#include "mrb_theorem.h"
33
#include "monotonic.h"
4+
#include <fcntl.h>
45

56
mrb_value mrb_mruby_bin_theorem_monotonic(mrb_state* mrb, mrb_value self)
67
{
78
return mrb_float_value(mrb, monotonic_seconds());
89
}
910

11+
mrb_value mrb_io_nonblock(mrb_state* mrb, mrb_value self)
12+
{
13+
int fd = mrb_int(mrb, mrb_funcall(mrb, self, "fileno", 0, NULL));
14+
int flags = fcntl(fd, F_GETFL, 0);
15+
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
16+
return mrb_nil_value();
17+
}
18+
1019
void mrb_mruby_bin_theorem_gem_init(mrb_state* mrb)
1120
{
21+
struct RClass* ioclass = mrb_class_get(mrb, "IO");
22+
mrb_define_method(mrb, ioclass, "nonblock!", mrb_io_nonblock, MRB_ARGS_NONE());
23+
1224
struct RClass* class = mrb_define_module(mrb, "Theorem");
1325
mrb_define_class_method(mrb, class, "monotonic", mrb_mruby_bin_theorem_monotonic, MRB_ARGS_NONE());
1426
}

test/theorem/matchers.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Tests
2+
class MatchersTest < Base
3+
let(:tests) do
4+
Class.new do
5+
include Fixture
6+
include Matchers
7+
8+
test "errors - eql" do
9+
expect("foo").to eql("bar")
10+
end
11+
12+
test "errors - include" do
13+
expect(%w[one two three]).to include(%w[one four])
14+
end
15+
16+
test "errors - be" do
17+
expect(true).to be(false)
18+
end
19+
end
20+
end
21+
22+
test "errors" do
23+
res = tests.run!
24+
expect(res[0].error.message).to eql('"foo" does not equal "bar"')
25+
expect(res[1].error.message).to eql('["one", "two", "three"] does not include ["one", "four"]')
26+
expect(res[2].error.message).to match(/TrueClass\.object_id\(\d+\) does not equal FalseClass\.object_id\(\d+\)/)
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)