Created
August 25, 2012 02:47
-
-
Save benolee/3459320 to your computer and use it in GitHub Desktop.
a naive implementation of dataflow variables in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Usage: run tests with `ruby dataflow.rb` | |
| require 'bundler/setup' | |
| module DataFlow | |
| class BasicObject < ::BasicObject | |
| undef_method :== | |
| undef_method :equal? | |
| def raise(*args) | |
| ::Object.send(:raise, *args) | |
| end | |
| end | |
| class DataFlowVar < BasicObject | |
| def initialize | |
| @mutex = ::Mutex.new | |
| @locked = false | |
| end | |
| def value=(value) | |
| if @locked | |
| raise ::StandardError, "value already assigned" | |
| else | |
| @mutex.synchronize do | |
| @locked = true | |
| @value = value | |
| end | |
| end | |
| end | |
| def value | |
| @value | |
| end | |
| end | |
| end | |
| if __FILE__ == $0 | |
| require 'minitest/autorun' | |
| class DataFlowVarTest < MiniTest::Unit::TestCase | |
| def setup | |
| @var = DataFlow::DataFlowVar.new | |
| end | |
| def test_assignment | |
| @var.value = "foo" | |
| @var.value.must_equal "foo" | |
| end | |
| def test_double_assignment_raises | |
| @var.value = "foo" | |
| -> { @var.value = "bar" }.must_raise StandardError | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source :rubygems | |
| gem 'minitest' |
You should also move require 'bundler/setup' into the test condition.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This may be incorrect, but I think it is a good start for the next test:
Also nice library testing trick. Did you come up with that on your own?