Skip to content

Commit

Permalink
Run crystal tool format
Browse files Browse the repository at this point in the history
  • Loading branch information
lagerfeuer committed Jun 22, 2021
1 parent 8e4665a commit e6b3777
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 96 deletions.
4 changes: 2 additions & 2 deletions samples/middleware.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ chat = Chat.new
chat.send_message.add { |body, yielder| yielder.call(body.upcase) }

# Prepend our user name
chat.send_message.add { |body, yielder| yielder.call("Alice: #{body}")}
chat.send_message.add { |body, yielder| yielder.call("Alice: #{body}") }

# Send a message
chat.send_message.call("Hello") #=> 13
chat.send_message.call("Hello") # => 13
# Prints "Sending Alice: HELLO"
12 changes: 6 additions & 6 deletions samples/real_world.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DataSource
spawn do
loop do
changed.emit rand(VALUE_RANGE) # Result of a complex calculation
sleep rand(SLEEP_RANGE) # Next value will take a bit
sleep rand(SLEEP_RANGE) # Next value will take a bit
end
end
end
Expand All @@ -53,27 +53,27 @@ end
class DataApplication
DISPLAYS = 5

@displays = [ ] of DataDisplay
@displays = [] of DataDisplay

def initialize
# We're using an interval sink, meaning, we want to get notified if anything
# happened within one second.
@sink = Cute::IntervalSink(Nil).new(interval: 1.seconds)

# If something happened, then redraw the application
@sink.on{|hits| redraw hits.size}
@sink.on { |hits| redraw hits.size }

# Create something that can change
DISPLAYS.times{ @displays << create_display }
DISPLAYS.times { @displays << create_display }
redraw # Don't forget to draw something initially!
end

private def create_display
source = DataSource.new # We have a source
source = DataSource.new # We have a source
display = DataDisplay.new # And something to show incoming data

# If the source gets new data, supply it to the display
source.changed.on{|value| display.data = value}
source.changed.on { |value| display.data = value }

# If the display changes somehow, schedule a redraw
display.changed.on(@sink)
Expand Down
2 changes: 1 addition & 1 deletion samples/sink_synchronisation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sink = Cute::BufferSink(Int32).new(size: 5)
10.times do |index| # Create ten jobs
thing = SomethingComplex.new(index)
thing.finished.on(sink) # Connect the signal to our sink
thing.run # Run the complex calculation!
thing.run # Run the complex calculation!
end

# Ten jobs, at batches of five, so wait two times.
Expand Down
2 changes: 1 addition & 1 deletion samples/testing.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe Button do

# The signal spy inherits from an array, so verification is easy
clicked_spy.size.should eq 1
clicked_spy.first.should eq({ 4, 5 })
clicked_spy.first.should eq({4, 5})
end
end
end
2 changes: 1 addition & 1 deletion samples/wait.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Button
end

btn = Button.new
spawn{ btn.clicked.emit 5, 4 }
spawn { btn.clicked.emit 5, 4 }

puts "Waiting for a click"
x, y = btn.clicked.wait
Expand Down
10 changes: 5 additions & 5 deletions spec/buffer_sink_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ describe Cute::BufferSink do

emits = [] of Array(Int32)
empty = [] of Array(Int32)
subject.on{|x| emits << x}
subject.on { |x| emits << x }

subject.notify 1
subject.notify 2
subject.notify 3

Fiber.yield
emits.should eq [ [ 1, 2, 3 ] ]
emits.should eq [[1, 2, 3]]

(4..8).each{|i| subject.notify i}
(4..8).each { |i| subject.notify i }

Fiber.yield
emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
emits.should eq [[1, 2, 3], [4, 5, 6]]

subject.notify 9

Fiber.yield
emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
emits.should eq [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
end
end
end
4 changes: 2 additions & 2 deletions spec/connect_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private class Listener
end

def on_move(x, y)
@move_data = { x, y }
@move_data = {x, y}
end

def on_one(msg)
Expand Down Expand Up @@ -53,6 +53,6 @@ describe "Cute.connect" do
subject = Listener.new(widget)

widget.moved.emit(5, 6)
subject.move_data.should eq({ 5, 6 })
subject.move_data.should eq({5, 6})
end
end
12 changes: 6 additions & 6 deletions spec/interval_sink_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ describe Cute::IntervalSink do

emits = [] of Array(Int32)
empty = [] of Array(Int32)
subject.on{|x| emits << x}
subject.on { |x| emits << x }

subject.notify 1
subject.notify 2
subject.notify 3

sleep 20.milliseconds
emits.should eq [ [ 1, 2, 3 ] ]
emits.should eq [[1, 2, 3]]

(4..8).each{|i| subject.notify i}
(4..8).each { |i| subject.notify i }

sleep 20.milliseconds
emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6, 7, 8 ] ]
emits.should eq [[1, 2, 3], [4, 5, 6, 7, 8]]

subject.notify 9

sleep 20.milliseconds
emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6, 7, 8 ], [ 9 ] ]
emits.should eq [[1, 2, 3], [4, 5, 6, 7, 8], [9]]

# Emits nothing if no notifications are received
sleep 20.milliseconds
emits.should eq [ [ 1, 2, 3 ], [ 4, 5, 6, 7, 8 ], [ 9 ] ]
emits.should eq [[1, 2, 3], [4, 5, 6, 7, 8], [9]]
end
end
end
20 changes: 10 additions & 10 deletions spec/middleware_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./spec_helper"

private class Chat
getter sent = [ ] of String
getter sent = [] of String

Cute.middleware def send_message(body : String) : Int32
@sent << body
Expand All @@ -17,7 +17,7 @@ describe "Cute.middleware" do
describe "without arguments" do
it "works fine" do
chat = Chat.new
chat.ping.add{|y| y.call * 2}
chat.ping.add { |y| y.call * 2 }
chat.ping.call.should eq 123 * 2
end
end
Expand All @@ -27,34 +27,34 @@ describe "Cute.middleware" do
it "calls the method" do
chat = Chat.new
chat.send_message.call("Hello").should eq 5
chat.sent.should eq [ "Hello" ]
chat.sent.should eq ["Hello"]
end
end

context "if one middleware was added" do
it "calls the middleware, then the method" do
chat = Chat.new
chat.send_message.add{|m, y| y.call m.upcase}
chat.send_message.add { |m, y| y.call m.upcase }
chat.send_message.call("Hello").should eq 5
chat.sent.should eq [ "HELLO" ]
chat.sent.should eq ["HELLO"]
end
end

context "if multiple middlewares are added" do
it "calls the middlewares in order, then the method" do
chat = Chat.new
chat.send_message.add{|m, y| y.call m.upcase}
chat.send_message.add{|m, y| y.call "saying #{m}"}
chat.send_message.add { |m, y| y.call m.upcase }
chat.send_message.add { |m, y| y.call "saying #{m}" }
chat.send_message.call("Hello").should eq "saying HELLO".size
chat.sent.should eq [ "saying HELLO" ]
chat.sent.should eq ["saying HELLO"]
end
end

context "if a middleware doesn't call onwards" do
it "the next stage is not called" do
chat = Chat.new
chat.send_message.add{|m, y| -1}
chat.send_message.add{|m, y| y.call m.upcase}
chat.send_message.add { |m, y| -1 }
chat.send_message.add { |m, y| y.call m.upcase }
chat.send_message.call("Hello").should eq -1
chat.sent.empty?.should eq true
end
Expand Down
50 changes: 25 additions & 25 deletions spec/signal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ describe "Cute.signal" do
subject = Widget.new

calls = Array(Int32).new
subject.closed.on{ calls << 1 }
subject.closed.on{ calls << 2 }
subject.closed.on{ calls << 3 }
subject.closed.on { calls << 1 }
subject.closed.on { calls << 2 }
subject.closed.on { calls << 3 }

calls.size.should eq 0
subject.closed.emit
calls.should eq [ 1, 2, 3 ]
calls.should eq [1, 2, 3]
end

context "with async: true" do
it "calls the handlers in a different fiber" do
subject = Widget.new

fibers = Array(Fiber).new
subject.asynced.on{ fibers << Fiber.current }
subject.asynced.on{ fibers << Fiber.current; Fiber.yield }
subject.asynced.on { fibers << Fiber.current }
subject.asynced.on { fibers << Fiber.current; Fiber.yield }

subject.asynced.emit
Fiber.yield # Call slot fiber
Expand All @@ -86,7 +86,7 @@ describe "Cute.signal" do
widget.one.on(sink)
widget.one.emit "Foo"

sink.collected.should eq [ nil ]
sink.collected.should eq [nil]
end
end

Expand All @@ -98,7 +98,7 @@ describe "Cute.signal" do
widget.moved.on(sink)
widget.moved.emit 4, 5

sink.collected.should eq [ nil ]
sink.collected.should eq [nil]
end
end

Expand All @@ -110,7 +110,7 @@ describe "Cute.signal" do
widget.closed.on(sink)
widget.closed.emit

sink.collected.should eq [ nil ]
sink.collected.should eq [nil]
end
end

Expand All @@ -123,7 +123,7 @@ describe "Cute.signal" do
widget.one.emit "Foo"
widget.one.emit "Bar"

sink.collected.should eq [ "Foo", "Bar" ]
sink.collected.should eq ["Foo", "Bar"]
end
end

Expand All @@ -136,7 +136,7 @@ describe "Cute.signal" do
widget.moved.emit 4, 5
widget.moved.emit 6, 7

sink.collected.should eq [ { 4, 5 }, { 6, 7 } ]
sink.collected.should eq [{4, 5}, {6, 7}]
end
end
end
Expand All @@ -146,14 +146,14 @@ describe "Cute.signal" do
subject = Widget.new

calls = Array(Int32).new
subject.closed.on{ calls << 1 }
handler = subject.closed.on{ calls << 2 }
subject.closed.on{ calls << 3 }
subject.closed.on { calls << 1 }
handler = subject.closed.on { calls << 2 }
subject.closed.on { calls << 3 }

calls.size.should eq 0
subject.closed.disconnect(handler)
subject.closed.emit
calls.should eq [ 1, 3 ]
calls.should eq [1, 3]
end
end

Expand All @@ -162,8 +162,8 @@ describe "Cute.signal" do
subject = Widget.new

calls = Array(Int32).new
subject.closed.on{ calls << 1 }
subject.closed.on{ calls << 2 }
subject.closed.on { calls << 1 }
subject.closed.on { calls << 2 }

calls.empty?.should be_true
subject.closed.disconnect
Expand All @@ -177,7 +177,7 @@ describe "Cute.signal" do
subject = Widget.new

ch, handler = subject.one.new_channel
fut = future{ ch.receive }
fut = future { ch.receive }
subject.one.emit "Okay"

handler.should_not be_nil
Expand All @@ -189,19 +189,19 @@ describe "Cute.signal" do
subject = Widget.new

ch, handler = subject.moved.new_channel
fut = future{ ch.receive }
fut = future { ch.receive }
subject.moved.emit 4, 5

fut.completed?.should be_true
handler.should_not be_nil
fut.get.should eq({ 4, 5 })
fut.get.should eq({4, 5})
end

context "with no signal arguments" do
subject = Widget.new

ch, handler = subject.closed.new_channel
fut = future{ ch.receive }
fut = future { ch.receive }
subject.closed.emit

handler.should_not be_nil
Expand All @@ -213,23 +213,23 @@ describe "Cute.signal" do
context "with one signal argument" do
subject = Widget.new

spawn{ subject.one.emit "Okay" }
spawn { subject.one.emit "Okay" }
result = subject.one.wait
result.should eq "Okay"
end

context "with many signal arguments" do
subject = Widget.new

spawn{ subject.moved.emit 4, 5 }
spawn { subject.moved.emit 4, 5 }
result = subject.moved.wait
result.should eq({ 4, 5 })
result.should eq({4, 5})
end

context "with no signal arguments" do
subject = Widget.new

spawn{ subject.closed.emit }
spawn { subject.closed.emit }
subject.closed.wait
end
end
Expand Down
Loading

0 comments on commit e6b3777

Please sign in to comment.