Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit c5b6f63

Browse files
Fixing and adding writer. Integration tests for parser.
1 parent faa9636 commit c5b6f63

8 files changed

+148
-13
lines changed

lib/moodle_gift_parser.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
module MoodleGiftParser
22
require 'logger'
33

4-
require 'moodle_gift_parser/parser'
4+
require 'moodle_gift_parser/gift_error'
5+
require 'moodle_gift_parser/invalid_gift_format_error'
6+
57

68
require 'moodle_gift_parser/question'
79
require 'moodle_gift_parser/essay_question'
810
require 'moodle_gift_parser/multiple_choice_question'
911

10-
require 'moodle_gift_parser/gift_error'
11-
require 'moodle_gift_parser/invalid_gift_format_error'
12+
require 'moodle_gift_parser/parser'
1213

1314
class << self
1415
#http://stackoverflow.com/questions/11729456/how-do-i-log-correctly-inside-my-ruby-gem
@@ -18,7 +19,7 @@ class << self
1819
def logger
1920
@logger ||= Logger.new($stdout).tap do |log|
2021
log.progname = self.name
21-
log.level = Logger::INFO
22+
log.level = Logger::DEBUG #INFO
2223
end
2324
return @logger
2425
end

lib/moodle_gift_parser/multiple_choice_question.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def self.build_alternative(is_correct)
3636
current_alt.credit = 0
3737
end
3838
current_alt.content = ''
39-
current_alt.comment = ''
39+
current_alt.comment = nil
4040
return current_alt
4141
end
4242
end
@@ -109,7 +109,7 @@ def self.parse_options(options)
109109
if state == State::COMMENT
110110
raise MoodleGiftParser::InvalidGiftFormatError, "Two following comment sequences with '#' found."
111111
end
112-
112+
current_alt.comment = ''
113113
state = State::COMMENT
114114
hashes = 0
115115
end

lib/moodle_gift_parser/parser.rb

+20-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ def parse(content)
1414
question = ''
1515
empty_lines = 0
1616
content.each_line do |line|
17-
if line.length == 0
17+
if line.strip.empty?
1818
empty_lines+=1
1919
if empty_lines >= 2 && !question.empty?
20-
begin
21-
questions << build_question(question, category, comment)
22-
rescue => e
23-
raise MoodleGiftParser::InvalidGiftFormatError, "Error parsing question:#{e.message}\nQuestion: '#{question}'", e.backtrace
24-
end
20+
MoodleGiftParser.logger.debug {"Question finished: '#{question}'"}
21+
questions << build_question_wrapped(question, category, comment)
2522

2623
question = ''
2724
comment = ''
@@ -30,22 +27,38 @@ def parse(content)
3027
end
3128

3229
if line.start_with?(CATEGORY_PREFIX)
33-
category = line[CATEGORY_PREFIX.length..-1]
30+
category = line[CATEGORY_PREFIX.length..-1].strip
31+
MoodleGiftParser.logger.debug {"Found category: '#{category}'"}
3432
next
3533
end
3634

3735
if line.start_with?(COMMENT_PREFIX)
3836
#concat if multiline comment
3937
comment = comment + line[COMMENT_PREFIX.length..-1]
38+
MoodleGiftParser.logger.debug {"Found comment: '#{comment.strip}'"}
4039
next
4140
end
4241

4342
question = question + line
4443
end
4544

45+
if !question.empty?
46+
MoodleGiftParser.logger.debug {"Last question finished: '#{question}'"}
47+
questions << build_question_wrapped(question, category, comment)
48+
end
49+
4650
return questions
4751
end
4852

53+
private
54+
def build_question_wrapped(question, category, comment)
55+
begin
56+
return build_question(question, category, comment)
57+
rescue => e
58+
raise MoodleGiftParser::InvalidGiftFormatError, "Error parsing question:#{e.message}\nQuestion: '#{question}'", e.backtrace
59+
end
60+
end
61+
4962
private
5063
def build_question(question_str, category, comment)
5164
question_text = question_str.dup

lib/moodle_gift_parser/writer.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class MoodleGiftParser::Writer
2+
@output = nil
3+
4+
def initialize(output)
5+
self.output = output
6+
end
7+
8+
def write(questions)
9+
questions.each do |q|
10+
q_gift = q.to_gift
11+
@output << q_gift + "\n\n"
12+
end
13+
end
14+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// question: 0 name: Switch category to $system$/Default/Example Category/Sub Category
2+
$CATEGORY: $system$/Default/Example Category/Sub Category
3+
4+
5+
// question: 81658 name: 2017.1-U4S1-Q01
6+
::2017.1-U4S1-Q01::[html]<p><span>Example 1</span></p>\n<p><span>Therefore\: </span></p>\n<p><span>Question\:</span></p>{
7+
~<p><span>First</span></p>
8+
~<p><span>Second.</span></p>
9+
=<p><span>Correct.</span></p>#<p><span>Reason.</span></p>
10+
~<p><span>Fourth.</span></p>
11+
~<p><span>Fifth.</span></p>
12+
}
13+
14+
15+
// question: 81659 name: 2017.1-U4S1-Q02
16+
::2017.1-U4S1-Q02::[html]<p><span>Example1 (2015)\: </span></p>\n<p><span>( )\:</span></p>{
17+
~<p><span>V,V,F,V,V,V,F.</span></p>
18+
~<p><span>V,F,V,V,V,F,V.</span></p>
19+
=<p><span>V,V,V,V,V,V,V.</span></p>#<p><span>Reason</span></p>
20+
~<p><span>V,F,F,V,V,V,V.</span></p>
21+
~<p><span>F,V,V,V,V,V,V.</span></p>
22+
}
23+
24+
25+
$CATEGORY: $system$/Default/Example Category 2/Sub Category 2
26+
27+
28+
// question: 87569 name: 2017.1-Q03
29+
::2017.1-Q03::[html]<p>Image\:</p>\n<p><img src\="http\://missing.png" alt\="" width\="474" height\="282" /></p>\n<p>Figure\:</p>{
30+
~<p>First.</p>
31+
=<p>Second.</p>
32+
~<p>Third “o”.</p>
33+
~<p>Fourth.</p>
34+
~<p>Fifth.</p>
35+
####<p>Generic comment!.</p>
36+
}

spec/parser_integration_spec.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'spec_helper'
2+
3+
describe 'Parse file' do
4+
before(:context) do
5+
@parser = MoodleGiftParser::Parser.new
6+
end
7+
8+
after(:example) do
9+
expect(@questions).to have(3).items
10+
11+
q1 = @questions[0]
12+
expect(q1).to be_a(MoodleGiftParser::MultipleChoiceQuestion)
13+
expect(q1.category).to eq('$system$/Default/Example Category/Sub Category')
14+
expect(q1.title).to eq('2017.1-U4S1-Q01')
15+
expect(q1.markup).to eq('html')
16+
expect(q1.content).to eq('<p><span>Example 1</span></p>\n<p><span>Therefore\: </span></p>\n<p><span>Question\:</span></p>')
17+
expect(q1.alternatives).to have(5).items
18+
alt1 = q1.alternatives[0]
19+
expect(alt1.content).to eq('<p><span>First</span></p>')
20+
expect(alt1.correct).to eq(false)
21+
expect(alt1.comment).to eq(nil)
22+
alt3 = q1.alternatives[2]
23+
expect(alt3.content).to eq('<p><span>Correct.</span></p>')
24+
expect(alt3.correct).to eq(true)
25+
expect(alt3.comment).to eq('<p><span>Reason.</span></p>')
26+
27+
q2 = @questions[1]
28+
expect(q2).to be_a(MoodleGiftParser::MultipleChoiceQuestion)
29+
expect(q2.category).to eq('$system$/Default/Example Category/Sub Category')
30+
31+
q3 = @questions[2]
32+
expect(q3).to be_a(MoodleGiftParser::MultipleChoiceQuestion)
33+
expect(q3.category).to eq('$system$/Default/Example Category 2/Sub Category 2')
34+
expect(q3.generic_comment).to eq('<p>Generic comment!.</p>')
35+
end
36+
37+
it 'should should parse file correctly' do
38+
path = File.expand_path '../example/example-gift-multiple-choice.gift', __FILE__
39+
File.open(path, 'r') do |file|
40+
@questions = @parser.parse(file)
41+
end
42+
end
43+
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
RSpec.configure do |config|
1010
# config.color_enabled = true
1111
config.formatter = 'documentation'
12+
#RAILS 5:config.file_fixture_path = "spec/example"
1213
end

spec/writer_single_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper'
2+
3+
describe 'Write MultipleChoiceQuestion' do
4+
before(:example) do
5+
@question = MoodleGiftParser::MultipleChoiceQuestion.new
6+
@question.category = 'category'
7+
@question.content = 'Content'
8+
@question.markup = 'html'
9+
@question.alternatives
10+
end
11+
12+
it 'should write without comment' do
13+
14+
end
15+
16+
it 'should write with comment' do
17+
18+
end
19+
20+
it 'should write with category' do
21+
22+
end
23+
24+
it 'should write without title and markup' do
25+
26+
end
27+
end

0 commit comments

Comments
 (0)