nerd_quiz 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/nerd_quiz/question.rb +4 -2
- data/lib/nerd_quiz/quiz.rb +7 -2
- data/lib/nerd_quiz/version.rb +1 -1
- data/spec/nerd_quiz/question_spec.rb +8 -8
- data/spec/nerd_quiz/quiz_spec.rb +22 -14
- metadata +9 -9
data/lib/nerd_quiz/question.rb
CHANGED
@@ -47,13 +47,15 @@ module NerdQuiz
|
|
47
47
|
text << @parsed["text"]
|
48
48
|
(1..4).each do |i|
|
49
49
|
key = "a#{i}"
|
50
|
-
|
50
|
+
# When the answer has significant whitespace, wrap the answer in quotes
|
51
|
+
answer = @parsed[key] =~ /^\s/ || @parsed[key] =~ /\s$/ ? "\"#{@parsed[key]}\"" : @parsed[key]
|
52
|
+
text << "#{i}) #{answer}"
|
51
53
|
end
|
52
54
|
@question[:text] = text.join("\n")
|
53
55
|
end
|
54
56
|
|
55
57
|
def extract_answer
|
56
|
-
@question[:answer] = @parsed["right_answer"]
|
58
|
+
@question[:answer] = @parsed["right_answer"][1]
|
57
59
|
end
|
58
60
|
|
59
61
|
def extract_label
|
data/lib/nerd_quiz/quiz.rb
CHANGED
@@ -30,6 +30,7 @@ module NerdQuiz
|
|
30
30
|
def ask
|
31
31
|
out("Question #{@scorecard.next_question + 1} #{label}:", :blue)
|
32
32
|
out(question)
|
33
|
+
out('>> ', :blue, :bold, :no_new_line)
|
33
34
|
end
|
34
35
|
|
35
36
|
def listen
|
@@ -45,7 +46,10 @@ module NerdQuiz
|
|
45
46
|
end
|
46
47
|
|
47
48
|
def reply
|
48
|
-
@input.gets
|
49
|
+
reply = @input.gets
|
50
|
+
# Ctrl-D (EOF) user action cause @input.gets to be nil
|
51
|
+
# On this action, lets exit like the user asked
|
52
|
+
reply.nil? ? exit : reply.strip
|
49
53
|
end
|
50
54
|
|
51
55
|
def start
|
@@ -81,7 +85,8 @@ module NerdQuiz
|
|
81
85
|
bold = args[2].is_a?(Symbol) ? true : false
|
82
86
|
text = set_color(text, color, bold)
|
83
87
|
end
|
84
|
-
|
88
|
+
method = args[3].nil? ? :puts : :print
|
89
|
+
@output.send(method, text)
|
85
90
|
end
|
86
91
|
|
87
92
|
def handle_signals
|
data/lib/nerd_quiz/version.rb
CHANGED
@@ -3,16 +3,16 @@ require 'spec_helper'
|
|
3
3
|
module NerdQuiz
|
4
4
|
describe Question do
|
5
5
|
describe '.get' do
|
6
|
-
let(:path) { File.expand_path('../../../NerdPursuit/questions/
|
6
|
+
let(:path) { File.expand_path('../../../NerdPursuit/questions/kernel-sprintf-2.json', __FILE__) }
|
7
7
|
let(:text) do
|
8
|
-
"
|
9
|
-
"
|
10
|
-
"
|
11
|
-
"
|
12
|
-
"
|
8
|
+
"What's the output of '%-020s' % 'dude is here'?\n" +
|
9
|
+
"1) dude is here\n" +
|
10
|
+
"2) \"dude is here \"\n" +
|
11
|
+
"3) \" dude is here\"\n" +
|
12
|
+
"4) dude is"
|
13
13
|
end
|
14
|
-
let(:answer) { "
|
15
|
-
let(:label) { "(
|
14
|
+
let(:answer) { "2" }
|
15
|
+
let(:label) { "(backend - ruby)" }
|
16
16
|
|
17
17
|
subject { Question.get(path) }
|
18
18
|
|
data/spec/nerd_quiz/quiz_spec.rb
CHANGED
@@ -5,7 +5,7 @@ module NerdQuiz
|
|
5
5
|
let(:question) { 'What is the answer to life, the universe and everything?' }
|
6
6
|
let(:answer) { '42' }
|
7
7
|
let(:label) { '(culture)' }
|
8
|
-
let(:output) { double('output').as_null_object }
|
8
|
+
let(:output) { double('output', :send => "").as_null_object }
|
9
9
|
let(:input) { double('input').as_null_object }
|
10
10
|
let(:scorecard) { NerdQuiz::Scorecard.new }
|
11
11
|
subject { Quiz.new(input, output, scorecard) }
|
@@ -17,18 +17,18 @@ module NerdQuiz
|
|
17
17
|
subject.stub(:answer).and_return(answer)
|
18
18
|
end
|
19
19
|
|
20
|
-
# Only public API is the run loop, so it must be run for each test
|
21
|
-
after(:each) do
|
22
|
-
subject.run
|
23
|
-
end
|
24
|
-
|
25
20
|
describe '#run' do
|
21
|
+
# Only public API is the run loop, so it must be run for each test
|
22
|
+
after(:each) do
|
23
|
+
subject.run
|
24
|
+
end
|
25
|
+
|
26
26
|
it 'loads all the questions' do
|
27
27
|
subject.should_receive(:questions).once
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'sends a welcome message' do
|
31
|
-
output.should_receive(:
|
31
|
+
output.should_receive(:send).with(:puts, "\e[34mWelcome to Nerd Quiz\e[0m")
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'asks all the questions' do
|
@@ -37,7 +37,7 @@ module NerdQuiz
|
|
37
37
|
|
38
38
|
it 'labels each question' do
|
39
39
|
(1..10).each do |i|
|
40
|
-
output.should_receive(:
|
40
|
+
output.should_receive(:send).with(:puts, "\e[34mQuestion #{i} (culture):\e[0m").once
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -56,7 +56,7 @@ module NerdQuiz
|
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'notifies it received the right answer' do
|
59
|
-
output.should_receive(:
|
59
|
+
output.should_receive(:send).with(:puts, "\e[1m\e[32mRight!\e[0m").exactly(10).times
|
60
60
|
end
|
61
61
|
|
62
62
|
it 'tallies the right answer' do
|
@@ -70,11 +70,11 @@ module NerdQuiz
|
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'notifies it received the wrong answer' do
|
73
|
-
output.should_receive(:
|
73
|
+
output.should_receive(:send).with(:puts, "\e[1m\e[31mWrong!\e[0m").exactly(10).times
|
74
74
|
end
|
75
75
|
|
76
76
|
it 'and displays the right answer' do
|
77
|
-
output.should_receive(:
|
77
|
+
output.should_receive(:send).with(:puts, "\e[31mYou should have answered \e[0m\e[1m\e[31m42\e[0m").exactly(10).times
|
78
78
|
end
|
79
79
|
|
80
80
|
it 'tallies the wrong answer' do
|
@@ -84,21 +84,29 @@ module NerdQuiz
|
|
84
84
|
end
|
85
85
|
|
86
86
|
it 'proclaims the quiz over' do
|
87
|
-
output.should_receive(:
|
87
|
+
output.should_receive(:send).with(:puts, "\e[34mThanks For Playing!\e[0m")
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'displays the final score' do
|
91
91
|
scorecard.stub(:score).and_return('10/10')
|
92
|
-
output.should_receive(:
|
92
|
+
output.should_receive(:send).with(:puts, "\e[33mFinal Score 10/10\e[0m")
|
93
93
|
end
|
94
94
|
|
95
95
|
it 'always says bye' do
|
96
|
-
output.should_receive(:
|
96
|
+
output.should_receive(:send).with(:puts, "\e[1m\e[34mBye!\e[0m")
|
97
97
|
end
|
98
98
|
|
99
99
|
it 'catches SIGINTs' do
|
100
100
|
Signal.should_receive(:trap).with('INT', 'EXIT').once
|
101
101
|
end
|
102
102
|
end
|
103
|
+
|
104
|
+
it 'exits when sent Ctrl-D (EOF)' do
|
105
|
+
lambda {
|
106
|
+
input.stub(:gets).and_return(nil)
|
107
|
+
output.should_receive(:send).with(:puts, "\e[1m\e[34mBye!\e[0m")
|
108
|
+
subject.run
|
109
|
+
}.should raise_error(SystemExit)
|
110
|
+
end
|
103
111
|
end
|
104
112
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nerd_quiz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: yajl-ruby
|
16
|
-
requirement: &
|
16
|
+
requirement: &70271390761360 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70271390761360
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70271390760620 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70271390760620
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70271390759980 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70271390759980
|
47
47
|
description: Test your nerd skills by answering NerdPursuit questions!
|
48
48
|
email:
|
49
49
|
- sfw@simeonfosterwillbanks.com
|
@@ -270,7 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
270
270
|
version: '0'
|
271
271
|
segments:
|
272
272
|
- 0
|
273
|
-
hash:
|
273
|
+
hash: -61159159520021561
|
274
274
|
requirements: []
|
275
275
|
rubyforge_project: nerd_quiz
|
276
276
|
rubygems_version: 1.8.11
|