Skip to content

Commit 8736877

Browse files
tohlhRichDom2185
andauthored
Language options (#1247)
* Add options field * Add options field * Fix format * Update some test cases * Update test cases * Fix XML factory * Revert undocumented XML path change * Simplify logic --------- Co-authored-by: Richard Dominick <[email protected]>
1 parent 40cd12a commit 8736877

File tree

6 files changed

+46
-26
lines changed

6 files changed

+46
-26
lines changed

lib/cadet/assessments/library.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ defmodule Cadet.Assessments.Library do
1212
field(:variant, :string, default: nil)
1313
field(:exec_time_ms, :integer, default: 1000)
1414
field(:globals, :map, default: %{})
15+
field(:language_options, :map, default: %{})
1516
embeds_one(:external, ExternalLibrary, on_replace: :update)
1617
end
1718

1819
@required_fields ~w(chapter)a
19-
@optional_fields ~w(globals variant exec_time_ms)a
20+
@optional_fields ~w(globals variant language_options exec_time_ms)a
2021
@required_embeds ~w(external)a
2122

2223
def changeset(library, params \\ %{}) do

lib/cadet/jobs/xml_parser.ex

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ defmodule Cadet.Updater.XMLParser do
133133

134134
@spec process_questions(String.t()) :: {:ok, [map()]} | {:error, String.t()}
135135
defp process_questions(xml) do
136-
default_library = xpath(xml, ~x"//TASK/DEPLOYMENT"e)
137-
default_grading_library = xpath(xml, ~x"//TASK/GRADERDEPLOYMENT"e)
136+
default_library = xpath(xml, ~x"//TASK/PROGRAMMINGLANGUAGE"e)
137+
default_grading_library = xpath(xml, ~x"//TASK/GRADERPROGRAMMINGLANGUAGE"e)
138138

139139
questions_params =
140140
xml
@@ -278,22 +278,23 @@ defmodule Cadet.Updater.XMLParser do
278278

279279
@spec process_question_library(map(), any(), any()) :: map() | {:error, String.t()}
280280
defp process_question_library(question, default_library, default_grading_library) do
281-
library = xpath(question[:entity], ~x"./DEPLOYMENT"o) || default_library
281+
library = xpath(question[:entity], ~x"./PROGRAMMINGLANGUAGE"o) || default_library
282282

283283
grading_library =
284-
xpath(question[:entity], ~x"./GRADERDEPLOYMENT"o) || default_grading_library || library
284+
xpath(question[:entity], ~x"./GRADERPROGRAMMINGLANGUAGE"o) || default_grading_library ||
285+
library
285286

286287
if library do
287288
question
288-
|> Map.put(:library, process_question_library(library))
289-
|> Map.put(:grading_library, process_question_library(grading_library))
289+
|> Map.put(:library, parse_programming_language(library))
290+
|> Map.put(:grading_library, parse_programming_language(grading_library))
290291
else
291-
{:error, "Missing DEPLOYMENT"}
292+
{:error, "Missing PROGRAMMINGLANGUAGE"}
292293
end
293294
end
294295

295-
@spec process_question_library(any()) :: map()
296-
defp process_question_library(library_entity) do
296+
@spec parse_programming_language(any()) :: map()
297+
defp parse_programming_language(library_entity) do
297298
globals =
298299
library_entity
299300
|> xpath(
@@ -313,6 +314,13 @@ defmodule Cadet.Updater.XMLParser do
313314
symbols: ~x"./SYMBOL/text()"sl
314315
)
315316

317+
options_list =
318+
library_entity
319+
|> xpath(~x"./OPTION"el, key: ~x"./@key"s, value: ~x"./@value"s)
320+
321+
options_map =
322+
options_list |> Map.new(&{&1.key, &1.value})
323+
316324
library_entity
317325
|> xpath(
318326
~x"."e,
@@ -322,6 +330,7 @@ defmodule Cadet.Updater.XMLParser do
322330
)
323331
|> Map.put(:globals, globals)
324332
|> Map.put(:external, external)
333+
|> Map.put(:language_options, options_map)
325334
end
326335

327336
@spec process_charlist(charlist() | nil) :: String.t() | nil

lib/cadet_web/helpers/assessments_helpers.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ defmodule CadetWeb.AssessmentsHelpers do
1010
variant: :variant,
1111
execTimeMs: :exec_time_ms,
1212
globals: :globals,
13-
external: &build_external_library(%{external_library: &1.external})
13+
external: &build_external_library(%{external_library: &1.external}),
14+
languageOptions: :language_options
1415
})
1516
end
1617

test/cadet/updater/xml_parser_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,21 @@ defmodule Cadet.Updater.XMLParserTest do
221221
end
222222
end
223223

224-
test "missing DEPLOYMENT", %{
224+
test "missing PROGRAMMINGLANGUAGE", %{
225225
questions: questions,
226226
course: course,
227227
assessments_with_config: assessments_with_config
228228
} do
229229
for {assessment, assessment_config} <- assessments_with_config do
230-
xml = XMLGenerator.generate_xml_for(assessment, questions, no_deployment: true)
230+
xml = XMLGenerator.generate_xml_for(assessment, questions, no_programminglanguage: true)
231231

232232
assert capture_log(fn ->
233233
assert(
234234
XMLParser.parse_xml(xml, course.id, assessment_config.id) ==
235-
{:error, {:bad_request, "Missing DEPLOYMENT"}}
235+
{:error, {:bad_request, "Missing PROGRAMMINGLANGUAGE"}}
236236
)
237237
end) =~
238-
"Missing DEPLOYMENT"
238+
"Missing PROGRAMMINGLANGUAGE"
239239
end
240240
end
241241

test/cadet_web/admin_controllers/admin_grading_controller_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
306306
"symbols" => &1.question.library.external.symbols
307307
},
308308
"execTimeMs" => &1.question.library.exec_time_ms,
309+
"languageOptions" => %{},
309310
"variant" => &1.question.library.variant
310311
},
311312
"maxXp" => &1.question.max_xp,
@@ -347,6 +348,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
347348
"symbols" => &1.question.library.external.symbols
348349
},
349350
"execTimeMs" => &1.question.library.exec_time_ms,
351+
"languageOptions" => %{},
350352
"variant" => &1.question.library.variant
351353
},
352354
"maxXp" => &1.question.max_xp,
@@ -398,6 +400,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
398400
"symbols" => &1.question.library.external.symbols
399401
},
400402
"execTimeMs" => &1.question.library.exec_time_ms,
403+
"languageOptions" => %{},
401404
"variant" => &1.question.library.variant
402405
},
403406
"maxXp" => &1.question.max_xp,
@@ -1327,6 +1330,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
13271330
"symbols" => &1.question.library.external.symbols
13281331
},
13291332
"execTimeMs" => &1.question.library.exec_time_ms,
1333+
"languageOptions" => %{},
13301334
"variant" => &1.question.library.variant
13311335
},
13321336
"maxXp" => &1.question.max_xp,
@@ -1368,6 +1372,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
13681372
"symbols" => &1.question.library.external.symbols
13691373
},
13701374
"execTimeMs" => &1.question.library.exec_time_ms,
1375+
"languageOptions" => %{},
13711376
"variant" => &1.question.library.variant
13721377
},
13731378
"content" => &1.question.question.content,
@@ -1419,6 +1424,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
14191424
"symbols" => &1.question.library.external.symbols
14201425
},
14211426
"execTimeMs" => &1.question.library.exec_time_ms,
1427+
"languageOptions" => %{},
14221428
"variant" => &1.question.library.variant
14231429
},
14241430
"maxXp" => &1.question.max_xp,

test/support/xml_generator.ex

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ defmodule Cadet.Test.XMLGenerator do
1919
) do
2020
assessment_wide_library =
2121
if opts[:library] do
22-
process_library(opts[:library], using: &deployment/2, no_deployment: opts[:no_deployment])
22+
process_library(opts[:library],
23+
using: &programminglanguage/2,
24+
no_deployment: opts[:no_programminglanguage]
25+
)
2326
else
2427
[]
2528
end
@@ -28,8 +31,8 @@ defmodule Cadet.Test.XMLGenerator do
2831
if opts[:grading_library] do
2932
process_library(
3033
opts[:grading_library],
31-
using: &graderdeployment/2,
32-
no_deployment: opts[:no_deployment]
34+
using: &graderprogramminglanguage/2,
35+
no_deployment: opts[:no_programminglanguage]
3336
)
3437
else
3538
[]
@@ -63,13 +66,13 @@ defmodule Cadet.Test.XMLGenerator do
6366
process_question_by_question_type(question) ++
6467
process_library(
6568
question.library,
66-
using: &deployment/2,
67-
no_deployment: opts[:no_deployment]
69+
using: &programminglanguage/2,
70+
no_deployment: opts[:no_programminglanguage]
6871
) ++
6972
process_library(
7073
question.grading_library,
71-
using: &graderdeployment/2,
72-
no_deployment: opts[:no_deployment]
74+
using: &graderprogramminglanguage/2,
75+
no_deployment: opts[:no_programminglanguage]
7376
)
7477
)
7578
end
@@ -171,12 +174,12 @@ defmodule Cadet.Test.XMLGenerator do
171174
{"VOTING", map_permit_keys(raw_attr, ~w(assessment_number reveal_hours token_divider)a)}
172175
end
173176

174-
defp deployment(raw_attrs, children) do
175-
{"DEPLOYMENT", map_permit_keys(raw_attrs, ~w(interpreter)a), children}
177+
defp programminglanguage(raw_attrs, children) do
178+
{"PROGRAMMINGLANGUAGE", map_permit_keys(raw_attrs, ~w(interpreter)a), children}
176179
end
177180

178-
defp graderdeployment(raw_attrs, children) do
179-
{"GRADERDEPLOYMENT", map_permit_keys(raw_attrs, ~w(interpreter)a), children}
181+
defp graderprogramminglanguage(raw_attrs, children) do
182+
{"GRADERPROGRAMMINGLANGUAGE", map_permit_keys(raw_attrs, ~w(interpreter)a), children}
180183
end
181184

182185
defp external(raw_attrs, children) do

0 commit comments

Comments
 (0)