sus 0.1.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.
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'io/console'
24
+ require 'stringio'
25
+
26
+ module Sus
27
+ # Styled output output.
28
+ module Output
29
+ class Null
30
+ def initialize
31
+ end
32
+
33
+ def indent
34
+ end
35
+
36
+ def outdent
37
+ end
38
+
39
+ def indented
40
+ yield
41
+ end
42
+
43
+ def write(*arguments)
44
+ # Do nothing.
45
+ end
46
+
47
+ def puts(*arguments)
48
+ # Do nothing.
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ module Sus
24
+ module Output
25
+ class Status
26
+ def self.register(output)
27
+ output[:free] ||= output.style(:blue)
28
+ output[:busy] ||= output.style(:orange)
29
+ end
30
+
31
+ def initialize(state = :free, context = nil)
32
+ @state = state
33
+ @context = context
34
+ @index = 0
35
+ end
36
+
37
+ INDICATORS = {
38
+ busy: ['◑', '◒', '◐', '◓'],
39
+ free: ['◌']
40
+ }
41
+
42
+ def update(state, context = nil)
43
+ @state = state
44
+ @context = context
45
+ end
46
+
47
+ def indicator
48
+ if indicators = INDICATORS[@state]
49
+ return indicators[(Time.now.to_f * 10) % indicators.size]
50
+ end
51
+
52
+ return " "
53
+ end
54
+
55
+ def print(output)
56
+ message = @message
57
+
58
+ output.write(
59
+ @state, self.indicator, " "
60
+ )
61
+
62
+ output.write(@context)
63
+
64
+ output.puts
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'io/console'
24
+ require_relative 'buffered'
25
+
26
+ module Sus
27
+ # Styled io io.
28
+ module Output
29
+ class Text
30
+ def initialize(io)
31
+ @io = io
32
+ @styles = {reset: self.reset}
33
+
34
+ @indent = String.new
35
+ @styles[:indent] = @indent
36
+ end
37
+
38
+ INDENTATION = "\t"
39
+
40
+ def indent
41
+ @indent << INDENTATION
42
+ end
43
+
44
+ def outdent
45
+ @indent.slice!(INDENTATION)
46
+ end
47
+
48
+ def indented
49
+ self.indent
50
+ yield
51
+ ensure
52
+ self.outdent
53
+ end
54
+
55
+ def interactive?
56
+ @io.tty?
57
+ end
58
+
59
+ attr :io
60
+
61
+ def [] key
62
+ @styles[key]
63
+ end
64
+
65
+ def []= key, value
66
+ @styles[key] = value
67
+ end
68
+
69
+ def size
70
+ [24, 80]
71
+ end
72
+
73
+ def width
74
+ size.last
75
+ end
76
+
77
+ def colors?
78
+ false
79
+ end
80
+
81
+ def style(foreground, background = nil, *attributes)
82
+ end
83
+
84
+ def reset
85
+ end
86
+
87
+ # Print out the given arguments.
88
+ # When the argument is a symbol, look up the style and inject it into the io stream.
89
+ # When the argument is a proc/lambda, call it with self as the argument.
90
+ # When the argument is anything else, write it directly to the io.
91
+ def write(*arguments)
92
+ arguments.each do |argument|
93
+ case argument
94
+ when Symbol
95
+ @io.write(self[argument])
96
+ when Proc
97
+ argument.call(self)
98
+ else
99
+ if argument.respond_to?(:print)
100
+ argument.print(self)
101
+ else
102
+ @io.write(argument)
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ # Print out the arguments as per {#print}, followed by the reset sequence and a newline.
109
+ def puts(*arguments)
110
+ write(*arguments)
111
+ @io.puts(self.reset)
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'io/console'
24
+
25
+ require_relative 'text'
26
+
27
+ module Sus
28
+ # Styled output output.
29
+ module Output
30
+ class XTerm < Text
31
+ COLORS = {
32
+ black: 0,
33
+ red: 1,
34
+ green: 2,
35
+ yellow: 3,
36
+ blue: 4,
37
+ magenta: 5,
38
+ cyan: 6,
39
+ white: 7,
40
+ default: 9,
41
+ }
42
+
43
+ ATTRIBUTES = {
44
+ normal: 0,
45
+ bold: 1,
46
+ bright: 1,
47
+ faint: 2,
48
+ italic: 3,
49
+ underline: 4,
50
+ blink: 5,
51
+ reverse: 7,
52
+ hidden: 8,
53
+ }
54
+
55
+ def colors?
56
+ true
57
+ end
58
+
59
+ def size
60
+ @io.winsize
61
+ end
62
+
63
+ def style(foreground, background = nil, *attributes)
64
+ tokens = []
65
+
66
+ if foreground
67
+ tokens << 30 + COLORS.fetch(foreground)
68
+ end
69
+
70
+ if background
71
+ tokens << 40 + COLORS.fetch(background)
72
+ end
73
+
74
+ attributes.each do |attribute|
75
+ tokens << ATTRIBUTES.fetch(attribute){attribute.to_i}
76
+ end
77
+
78
+ return "\e[#{tokens.join(';')}m"
79
+ end
80
+
81
+ def reset
82
+ "\e[0m"
83
+ end
84
+ end
85
+ end
86
+ end
data/lib/sus/output.rb ADDED
@@ -0,0 +1,41 @@
1
+
2
+ require_relative 'output/bar'
3
+ require_relative 'output/text'
4
+ require_relative 'output/xterm'
5
+
6
+ require_relative 'output/null'
7
+
8
+ module Sus
9
+ module Output
10
+ def self.for(io)
11
+ if io.isatty
12
+ XTerm.new(io)
13
+ else
14
+ Text.new(io)
15
+ end
16
+ end
17
+
18
+ def self.default(io = $stderr)
19
+ output = self.for(io)
20
+
21
+ Output::Bar.register(output)
22
+
23
+ output[:context] = output.style(nil, nil, :bold)
24
+
25
+ output[:describe] = output.style(:cyan, nil, :bold)
26
+ output[:it] = output.style(:cyan)
27
+ output[:with] = output.style(:cyan)
28
+
29
+ output[:variable] = output.style(:blue, nil, :bold)
30
+
31
+ output[:passed] = output.style(:green, nil, :bold)
32
+ output[:failed] = output.style(:red, nil, :bold)
33
+
34
+ return output
35
+ end
36
+
37
+ def self.buffered
38
+ Buffered.new(Null.new)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require_relative 'output/bar'
24
+ require_relative 'output/status'
25
+ require_relative 'output/lines'
26
+
27
+ module Sus
28
+ class Progress
29
+ def self.now
30
+ ::Process.clock_gettime(Process::CLOCK_MONOTONIC)
31
+ end
32
+
33
+ def initialize(output, total = 0, minimum_output_duration: 1.0)
34
+ @output = output
35
+ @subject = subject
36
+
37
+ @start_time = Progress.now
38
+
39
+ if @output.interactive?
40
+ @bar = Output::Bar.new
41
+ @lines = Output::Lines.new(@output)
42
+ @lines[0] = @bar
43
+ end
44
+
45
+ @current = 0
46
+ @total = total
47
+ end
48
+
49
+ attr :subject
50
+ attr :current
51
+ attr :total
52
+
53
+ def duration
54
+ Progress.now - @start_time
55
+ end
56
+
57
+ def progress
58
+ @current.to_f / @total.to_f
59
+ end
60
+
61
+ def remaining
62
+ @total - @current
63
+ end
64
+
65
+ def average_duration
66
+ if @current > 0
67
+ duration / @current
68
+ end
69
+ end
70
+
71
+ def estimated_remaining_time
72
+ if average_duration = self.average_duration
73
+ average_duration * remaining
74
+ end
75
+ end
76
+
77
+ # Increase the amont of work done.
78
+ def increment(amount = 1)
79
+ @current += amount
80
+
81
+ @bar.update(@current, @total, self.to_s)
82
+ @lines.redraw(0)
83
+
84
+ return self
85
+ end
86
+
87
+ # Increase the total size of the progress.
88
+ def expand(amount = 1)
89
+ @total += amount
90
+
91
+ @bar.update(@current, @total, self.to_s)
92
+ @lines.redraw(0)
93
+
94
+ return self
95
+ end
96
+
97
+ def report(index, context, state)
98
+ @lines[index+1] = Output::Status.new(state, context)
99
+
100
+ return self
101
+ end
102
+
103
+ def clear
104
+ @lines.clear
105
+ end
106
+
107
+ def to_s
108
+ if estimated_remaining_time = self.estimated_remaining_time
109
+ "#{@current}/#{@total} completed in #{formatted_duration(self.duration)}, #{formatted_duration(estimated_remaining_time)} remaining"
110
+ else
111
+ "#{@current}/#{@total} completed"
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ def formatted_duration(duration)
118
+ seconds = duration.floor
119
+
120
+ if seconds < 60.0
121
+ return "#{seconds}s"
122
+ end
123
+
124
+ minutes = (duration / 60.0).floor
125
+ seconds = (seconds - (minutes * 60)).round
126
+
127
+ if minutes < 60.0
128
+ return "#{minutes}m#{seconds}s"
129
+ end
130
+
131
+ hours = (minutes / 60.0).floor
132
+ minutes = (minutes - (hours * 60)).round
133
+
134
+ if hours < 24.0
135
+ return "#{hours}h#{minutes}m"
136
+ end
137
+
138
+ days = (hours / 24.0).floor
139
+ hours = (hours - (days * 24)).round
140
+
141
+ return "#{days}d#{hours}h"
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,47 @@
1
+
2
+ require_relative 'base'
3
+
4
+ require_relative 'describe'
5
+ require_relative 'with'
6
+
7
+ require_relative 'it'
8
+
9
+ require_relative 'shared'
10
+ require_relative 'it_behaves_like'
11
+ require_relative 'include_context'
12
+
13
+ require_relative 'let'
14
+
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
+ module Sus
19
+ class Registry
20
+ # Create a top level scope with self as the instance:
21
+ def initialize(base = Sus.base("test registry"))
22
+ @base = base
23
+ end
24
+
25
+ attr :base
26
+
27
+ def load(path)
28
+ @base.describe(path, identity: Identity.new(path)) do
29
+ TOPLEVEL_CLASS_EVAL.call(self, path)
30
+ end
31
+ end
32
+
33
+ def call(assertions = Assertions.default)
34
+ @base.call(assertions)
35
+
36
+ return assertions
37
+ end
38
+
39
+ def each(...)
40
+ @base.each(...)
41
+ end
42
+
43
+ def children
44
+ @base.children
45
+ end
46
+ end
47
+ end
data/lib/sus/shared.rb ADDED
@@ -0,0 +1,22 @@
1
+
2
+ require_relative 'context'
3
+
4
+ module Sus
5
+ module Shared
6
+ attr_accessor :name
7
+ attr_accessor :block
8
+
9
+ def self.build(name, block)
10
+ base = Class.new
11
+ base.extend(Shared)
12
+ base.name = name
13
+ base.block = block
14
+
15
+ return base
16
+ end
17
+ end
18
+
19
+ def self.Shared(name, &block)
20
+ Shared.build(name, block)
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sus
4
+ VERSION = "0.1.0"
5
+ end
data/lib/sus/with.rb ADDED
@@ -0,0 +1,48 @@
1
+
2
+ require_relative 'context'
3
+
4
+ module Sus
5
+ module With
6
+ extend Context
7
+
8
+ attr_accessor :subject
9
+ attr_accessor :variables
10
+
11
+ def self.extended(base)
12
+ base.children = Hash.new
13
+ end
14
+
15
+ def self.build(parent, subject, variables, &block)
16
+ base = Class.new(parent)
17
+ base.extend(With)
18
+ base.subject = subject
19
+ base.description = subject
20
+ base.identity = Identity.nested(parent.identity, base.description)
21
+ base.variables = variables
22
+
23
+ variables.each do |key, value|
24
+ base.define_method(key, ->{value})
25
+ end
26
+
27
+ if block_given?
28
+ base.class_exec(&block)
29
+ end
30
+
31
+ return base
32
+ end
33
+
34
+ def print(output)
35
+ self.superclass.print(output)
36
+ output.write(
37
+ " with ", :with, self.description, :reset,
38
+ # " ", :variables, self.variables.inspect
39
+ )
40
+ end
41
+ end
42
+
43
+ module Context
44
+ def with(subject, **variables, &block)
45
+ add With.build(self, subject, variables, &block)
46
+ end
47
+ end
48
+ end
data/lib/sus.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sus/version'
4
+ require_relative 'sus/registry'
5
+ require_relative 'sus/assertions'
6
+
7
+ require_relative 'sus/expect'
8
+ require_relative 'sus/be'
9
+
10
+ require_relative 'sus/filter'