Skip to content

Commit

Permalink
Add tests to prove Calyx can handle weighted choices
Browse files Browse the repository at this point in the history
  • Loading branch information
tra38 authored and Tariq Ali committed Mar 25, 2016
1 parent ef92e31 commit 1d835dc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions spec/calyx_spec.rb
Expand Up @@ -179,4 +179,33 @@ class StockReport < Calyx::Grammar
expect(array.uniq.sort).to eq(["apple","orange"])
end

specify 'construct a Calyx::Grammar class with weighted choices' do
yaml_text = <<-EOF
start:
- [20%, 0.2]
- [80%, 0.8]
EOF
yaml = YAML.load(yaml_text)
filepath = "test.yml"
allow(YAML).to receive(:load_file).with(filepath).and_return(yaml)
grammar = Calyx::Grammar.load_yml(filepath)
array = []
10.times { array << grammar.generate }
expect(array.uniq.sort).to eq(["20%","80%"])
end

specify 'raise an error if weighted choices do not equal 1' do
yaml_text = <<-EOF
start:
- [90%, 0.9]
- [80%, 0.8]
EOF
yaml = YAML.load(yaml_text)
filepath = "test.yml"
allow(YAML).to receive(:load_file).with(filepath).and_return(yaml)
expect do
Calyx::Grammar.load_yml(filepath)
end.to raise_error('Weights must sum to 1')
end

end

0 comments on commit 1d835dc

Please sign in to comment.