sus 0.1.1 → 0.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f1df466d84d494d20a673f838f968b1d89456928dfea7a9ca7b72f50d3675345
4
- data.tar.gz: 0277ccbe0791b8c0e78e0301a9bd73704eb46177754710d516baf6e77a7fa125
3
+ metadata.gz: fd31357ac07052a2e6c93a7dc674d2245a567644434eaf8e1983dedd7f594be7
4
+ data.tar.gz: c33ba038edd7f9c22896a6e3c4b8fe7f8880d4c0e8361c3d44cbd74bba35939f
5
5
  SHA512:
6
- metadata.gz: 16b55cb766a2ab965d0e5cf136d448b3bfb2978d72761ef39e78379f40cd947769c26208e831ed8ea6432e6d902e01a9e1b69b391074758f7341b921d2b66e12
7
- data.tar.gz: c1e44c881562220f893067320d07f0a71f2906b8867bc6162a5618b534bbcc571cf693ead8152e854595141f6d7d93d4cb242c10b97de643f92be8bdc153b43f
6
+ metadata.gz: bfcd742a489b8ec128cd79ab6d631b1ba6bc45421563eeec51acb476648bc5bb821dc5fccbf0530032e5075879c8271a1be0981825e444d0893a407ca6ebb6fa
7
+ data.tar.gz: 6014eff61f90083dbf9210faf64d2c7fec25c67cc8e0afa1721f20d3d730f7d8feea22c36e0db308b5712fea9b5a75433bd01785c0bea5eeb20bdb9a3ac5d7e4
data/lib/sus/be.rb CHANGED
@@ -11,7 +11,7 @@ module Sus
11
11
 
12
12
  def call(assertions, subject)
13
13
  assertions.nested(self) do |assertions|
14
- assertions.assert(subject.public_send(*@arguments), self)
14
+ assertions.assert(subject.public_send(*@arguments), subject)
15
15
  end
16
16
  end
17
17
 
@@ -0,0 +1,53 @@
1
+
2
+ module Sus
3
+ class BeWithin
4
+ class Bounded
5
+ def initialize(range)
6
+ @range = range
7
+ end
8
+
9
+ def print(output)
10
+ output.write("be within ", :variable, @range)
11
+ end
12
+
13
+ def call(assertions, subject)
14
+ assertions.nested(self) do |assertions|
15
+ assertions.assert(@range.include?(subject), subject)
16
+ end
17
+ end
18
+ end
19
+
20
+ def initialize(tolerance)
21
+ @tolerance = tolerance
22
+ end
23
+
24
+ def of(value)
25
+ Bounded.new(Range.new(value - @tolerance, value + @tolerance))
26
+ end
27
+
28
+ def percent_of(value)
29
+ of(value * Rational(@tolerance, 100))
30
+ end
31
+
32
+ def print(output)
33
+ output.write("be within ", :variable, @tolerance)
34
+ end
35
+
36
+ def call(assertions, subject)
37
+ assertions.nested(self) do |assertions|
38
+ assertions.assert(subject < @tolerance, self)
39
+ end
40
+ end
41
+ end
42
+
43
+ class Base
44
+ def be_within(value)
45
+ case value
46
+ when Range
47
+ BeWithin::Bounded.new(value)
48
+ else
49
+ BeWithin.new(value)
50
+ end
51
+ end
52
+ end
53
+ end
data/lib/sus/describe.rb CHANGED
@@ -11,12 +11,12 @@ module Sus
11
11
  base.children = Hash.new
12
12
  end
13
13
 
14
- def self.build(parent, subject, identity: nil, &block)
14
+ def self.build(parent, subject, unique: true, &block)
15
15
  base = Class.new(parent)
16
16
  base.extend(Describe)
17
17
  base.subject = subject
18
- base.description = subject.inspect
19
- base.identity = identity || Identity.nested(parent.identity, base.description)
18
+ base.description = subject.to_s
19
+ base.identity = Identity.nested(parent.identity, base.description, unique: unique)
20
20
  base.define_method(:subject, ->{subject})
21
21
 
22
22
  if block_given?
@@ -35,8 +35,8 @@ module Sus
35
35
  end
36
36
 
37
37
  module Context
38
- def describe(...)
39
- add Describe.build(self, ...)
38
+ def describe(subject, **options, &block)
39
+ add Describe.build(self, subject, **options, &block)
40
40
  end
41
41
  end
42
42
  end
data/lib/sus/expect.rb CHANGED
@@ -30,59 +30,11 @@ module Sus
30
30
 
31
31
  return self
32
32
  end
33
-
34
- def to_throw(...)
35
- predicate = ThrowException.new(...)
36
-
37
- @assertions.nested(self, inverted: @inverted) do |assertions|
38
- predicate.call(assertions, @subject)
39
- end
40
-
41
- return self
42
- end
43
33
  end
44
34
 
45
35
  class Base
46
- def expect(subject)
47
- Expect.new(@assertions, subject)
48
- end
49
- end
50
-
51
- class ThrowException
52
- def initialize(exception_class, message = nil)
53
- @exception_class = exception_class
54
- @message = message
55
- end
56
-
57
- def call(assertions, value)
58
- assertions.nested(self) do |assertions|
59
- begin
60
- value.call
61
-
62
- # Didn't throw any exception, so the expectation failed:
63
- assertions.assert(false, self)
64
- rescue => exception
65
- # Did we throw the right kind of exception?
66
- if exception.is_a?(@exception_class)
67
- # Did it have the right message?
68
- if @message
69
- assertions.assert(@message === exception.message)
70
- else
71
- assertions.assert(true, self)
72
- end
73
- else
74
- raise
75
- end
76
- end
77
- end
78
- end
79
-
80
- def print(output)
81
- output << "throw exception " << output.style(@exception_class, :variable)
82
-
83
- if @message
84
- output << "with message " << output.style(@message, :variable)
85
- end
36
+ def expect(subject = nil, &block)
37
+ Expect.new(@assertions, subject || block)
86
38
  end
87
39
  end
88
40
  end
data/lib/sus/file.rb ADDED
@@ -0,0 +1,34 @@
1
+
2
+ require_relative 'context'
3
+
4
+ # This has to be done at the top level. It allows us to define constants within the given class while still retaining top-level constant resolution.
5
+ Sus::TOPLEVEL_CLASS_EVAL = ->(klass, path){klass.class_eval(::File.read(path), path)}
6
+
7
+ module Sus
8
+ module File
9
+ extend Context
10
+
11
+ attr_accessor :path
12
+
13
+ def self.extended(base)
14
+ base.children = Hash.new
15
+ end
16
+
17
+ def self.build(parent, path)
18
+ base = Class.new(parent)
19
+ base.extend(File)
20
+ base.description = path
21
+ base.identity = Identity.new(path)
22
+
23
+ TOPLEVEL_CLASS_EVAL.call(base, path)
24
+
25
+ return base
26
+ end
27
+ end
28
+
29
+ module Context
30
+ def file(path)
31
+ add File.build(self, path)
32
+ end
33
+ end
34
+ end
data/lib/sus/filter.rb CHANGED
@@ -16,7 +16,13 @@ module Sus
16
16
  end
17
17
 
18
18
  def insert(identity, context)
19
- @contexts[identity.key] = context
19
+ key = identity.key
20
+
21
+ if @contexts[key]
22
+ raise KeyError, "Assigning context to existing key: #{key.inspect}!"
23
+ else
24
+ @contexts[key] = context
25
+ end
20
26
  end
21
27
 
22
28
  def [] key
@@ -0,0 +1,41 @@
1
+
2
+ module Sus
3
+ class HaveDuration
4
+ def initialize(predicate)
5
+ @predicate = predicate
6
+ end
7
+
8
+ def print(output)
9
+ output.write("have duration ")
10
+ @predicate.print(output)
11
+ end
12
+
13
+ def call(assertions, subject)
14
+ assertions.nested(self) do |assertions|
15
+ duration = measure(subject)
16
+
17
+ @predicate.call(assertions, duration)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def measure(subject)
24
+ start_time = now
25
+
26
+ subject.call
27
+
28
+ return now - start_time
29
+ end
30
+
31
+ def now
32
+ ::Process.clock_gettime(Process::CLOCK_MONOTONIC)
33
+ end
34
+ end
35
+
36
+ class Base
37
+ def have_duration(...)
38
+ HaveDuration.new(...)
39
+ end
40
+ end
41
+ end
data/lib/sus/identity.rb CHANGED
@@ -7,6 +7,7 @@ module Sus
7
7
  self.new(location.path, name, location.lineno, parent, **options)
8
8
  end
9
9
 
10
+ # @parameter unique [Boolean | Symbol] Whether this identity is unique or needs a unique key/line number suffix.
10
11
  def initialize(path, name = nil, line = nil, parent = nil, unique: true)
11
12
  @path = path
12
13
  @name = name
@@ -55,7 +56,7 @@ module Sus
55
56
  unless @key
56
57
  key = Array.new
57
58
 
58
- append_unique_key(key, false)
59
+ append_unique_key(key)
59
60
 
60
61
  @key = key.join(':')
61
62
  end
@@ -65,15 +66,21 @@ module Sus
65
66
 
66
67
  protected
67
68
 
68
- def append_unique_key(key, unique = @unique)
69
+ def append_unique_key(key)
69
70
  if @parent
70
71
  @parent.append_unique_key(key)
71
72
  else
72
73
  key << @path
73
74
  end
74
75
 
75
- if @line
76
- key << @line unless unique
76
+ if @unique == true
77
+ # No key is needed because this identity is unique.
78
+ else
79
+ if @unique
80
+ key << @unique
81
+ elsif @line
82
+ key << @line
83
+ end
77
84
  end
78
85
  end
79
86
  end
data/lib/sus/it.rb CHANGED
@@ -7,7 +7,7 @@ module Sus
7
7
  base = Class.new(parent)
8
8
  base.extend(It)
9
9
  base.description = description
10
- base.identity = Identity.nested(parent.identity, base.description)
10
+ base.identity = Identity.nested(parent.identity, base.description, unique: false)
11
11
 
12
12
  if block_given?
13
13
  base.define_method(:call, &block)
@@ -11,11 +11,11 @@ module Sus
11
11
  base.children = Hash.new
12
12
  end
13
13
 
14
- def self.build(parent, shared)
14
+ def self.build(parent, shared, unique: false)
15
15
  base = Class.new(parent)
16
16
  base.extend(ItBehavesLike)
17
17
  base.description = shared.name
18
- base.identity = Identity.nested(parent.identity, base.description, unique: false)
18
+ base.identity = Identity.nested(parent.identity, base.description, unique: unique)
19
19
  base.class_exec(&shared.block)
20
20
  return base
21
21
  end
@@ -27,8 +27,8 @@ module Sus
27
27
  end
28
28
 
29
29
  module Context
30
- def it_behaves_like(shared)
31
- add ItBehavesLike.build(self, shared)
30
+ def it_behaves_like(shared, **options)
31
+ add ItBehavesLike.build(self, shared, **options)
32
32
  end
33
33
  end
34
34
  end
data/lib/sus/progress.rb CHANGED
@@ -78,8 +78,8 @@ module Sus
78
78
  def increment(amount = 1)
79
79
  @current += amount
80
80
 
81
- @bar.update(@current, @total, self.to_s)
82
- @lines.redraw(0)
81
+ @bar&.update(@current, @total, self.to_s)
82
+ @lines&.redraw(0)
83
83
 
84
84
  return self
85
85
  end
@@ -88,20 +88,20 @@ module Sus
88
88
  def expand(amount = 1)
89
89
  @total += amount
90
90
 
91
- @bar.update(@current, @total, self.to_s)
92
- @lines.redraw(0)
91
+ @bar&.update(@current, @total, self.to_s)
92
+ @lines&.redraw(0)
93
93
 
94
94
  return self
95
95
  end
96
96
 
97
97
  def report(index, context, state)
98
- @lines[index+1] = Output::Status.new(state, context)
98
+ @lines&.[]=(index+1, Output::Status.new(state, context))
99
99
 
100
100
  return self
101
101
  end
102
102
 
103
103
  def clear
104
- @lines.clear
104
+ @lines&.clear
105
105
  end
106
106
 
107
107
  def to_s
@@ -0,0 +1,49 @@
1
+ module Sus
2
+ class RaiseException
3
+ def initialize(exception_class = nil, message: nil)
4
+ @exception_class = exception_class
5
+ @message = message
6
+ end
7
+
8
+ def call(assertions, subject)
9
+ assertions.nested(self) do |assertions|
10
+ begin
11
+ subject.call
12
+
13
+ # Didn't throw any exception, so the expectation failed:
14
+ assertions.assert(false, self)
15
+ rescue => exception
16
+ # Did we throw the right kind of exception?
17
+ if exception.is_a?(@exception_class)
18
+ # Did it have the right message?
19
+ if @message
20
+ assertions.assert(@message === exception.message)
21
+ else
22
+ assertions.assert(true, self)
23
+ end
24
+ else
25
+ raise
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def print(output)
32
+ output.write("raise exception")
33
+
34
+ if @exception_class
35
+ output.write(" ", :variable, @exception_class, :reset)
36
+ end
37
+
38
+ if @message
39
+ output.write(" with message ", :variable, @message, :reset)
40
+ end
41
+ end
42
+ end
43
+
44
+ class Base
45
+ def raise_exception(...)
46
+ RaiseException.new(...)
47
+ end
48
+ end
49
+ end
data/lib/sus/registry.rb CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  require_relative 'base'
3
3
 
4
+ require_relative 'file'
4
5
  require_relative 'describe'
5
6
  require_relative 'with'
6
7
 
@@ -12,9 +13,6 @@ require_relative 'include_context'
12
13
 
13
14
  require_relative 'let'
14
15
 
15
- # This has to be done at the top level. It allows us to define constants within the given class while still retaining top-level constant resolution.
16
- TOPLEVEL_CLASS_EVAL = ->(klass, path){klass.class_eval(File.read(path), path)}
17
-
18
16
  module Sus
19
17
  class Registry
20
18
  # Create a top level scope with self as the instance:
@@ -25,9 +23,7 @@ module Sus
25
23
  attr :base
26
24
 
27
25
  def load(path)
28
- @base.describe(path, identity: Identity.new(path)) do
29
- TOPLEVEL_CLASS_EVAL.call(self, path)
30
- end
26
+ @base.file(path)
31
27
  end
32
28
 
33
29
  def call(assertions = Assertions.default)
data/lib/sus/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sus
4
- VERSION = "0.1.1"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/sus/with.rb CHANGED
@@ -12,12 +12,12 @@ module Sus
12
12
  base.children = Hash.new
13
13
  end
14
14
 
15
- def self.build(parent, subject, variables, &block)
15
+ def self.build(parent, subject, variables, unique: true, &block)
16
16
  base = Class.new(parent)
17
17
  base.extend(With)
18
18
  base.subject = subject
19
19
  base.description = subject
20
- base.identity = Identity.nested(parent.identity, base.description)
20
+ base.identity = Identity.nested(parent.identity, base.description, unique: unique)
21
21
  base.variables = variables
22
22
 
23
23
  variables.each do |key, value|
data/lib/sus.rb CHANGED
@@ -6,5 +6,9 @@ require_relative 'sus/assertions'
6
6
 
7
7
  require_relative 'sus/expect'
8
8
  require_relative 'sus/be'
9
+ require_relative 'sus/be_within'
10
+
11
+ require_relative 'sus/raise_exception'
12
+ require_relative 'sus/have_duration'
9
13
 
10
14
  require_relative 'sus/filter'
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-05 00:00:00.000000000 Z
11
+ date: 2021-12-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
15
15
  executables:
16
16
  - sus
17
+ - sus-parallel
17
18
  extensions: []
18
19
  extra_rdoc_files: []
19
20
  files:
@@ -23,10 +24,13 @@ files:
23
24
  - lib/sus/assertions.rb
24
25
  - lib/sus/base.rb
25
26
  - lib/sus/be.rb
27
+ - lib/sus/be_within.rb
26
28
  - lib/sus/context.rb
27
29
  - lib/sus/describe.rb
28
30
  - lib/sus/expect.rb
31
+ - lib/sus/file.rb
29
32
  - lib/sus/filter.rb
33
+ - lib/sus/have_duration.rb
30
34
  - lib/sus/identity.rb
31
35
  - lib/sus/include_context.rb
32
36
  - lib/sus/it.rb
@@ -41,6 +45,7 @@ files:
41
45
  - lib/sus/output/text.rb
42
46
  - lib/sus/output/xterm.rb
43
47
  - lib/sus/progress.rb
48
+ - lib/sus/raise_exception.rb
44
49
  - lib/sus/registry.rb
45
50
  - lib/sus/shared.rb
46
51
  - lib/sus/version.rb
@@ -65,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
70
  - !ruby/object:Gem::Version
66
71
  version: '0'
67
72
  requirements: []
68
- rubygems_version: 3.2.32
73
+ rubygems_version: 3.3.0.dev
69
74
  signing_key:
70
75
  specification_version: 4
71
76
  summary: A fast and scalable test runner.