From 9a4fce961bc5346732ad91169357e45722fc160d Mon Sep 17 00:00:00 2001 From: Vit Brunner Date: Mon, 10 Jun 2013 23:55:55 +0100 Subject: [PATCH] Add json reader and its tests --- source/_scripts.haml | 1 + source/scripts/reader/json.coffee | 16 ++++++++++++ source/scripts/tests/reader/json.coffee | 33 +++++++++++++++++++++++++ source/tests.haml | 1 + 4 files changed, 51 insertions(+) create mode 100644 source/scripts/reader/json.coffee create mode 100644 source/scripts/tests/reader/json.coffee diff --git a/source/_scripts.haml b/source/_scripts.haml index 0d69070..49d4aff 100644 --- a/source/_scripts.haml +++ b/source/_scripts.haml @@ -5,4 +5,5 @@ #{javascript_include_tag "app"} #{javascript_include_tag "services"} #{javascript_include_tag "reader/markdown"} +#{javascript_include_tag "reader/json"} #{javascript_include_tag "controllers"} diff --git a/source/scripts/reader/json.coffee b/source/scripts/reader/json.coffee new file mode 100644 index 0000000..358b0b6 --- /dev/null +++ b/source/scripts/reader/json.coffee @@ -0,0 +1,16 @@ +angular.module("sideBySide").factory("jsonReader", () -> + return (source) -> + parsed = eval('(' + source + ')') + + new_content = [] + for verse in parsed.content + if typeof(verse) == "string" + new_content.push({ section: "", text: verse }) + else + if "section" not of verse + verse.section = "" + new_content.push(verse) + + parsed.content = new_content + return parsed +) diff --git a/source/scripts/tests/reader/json.coffee b/source/scripts/tests/reader/json.coffee new file mode 100644 index 0000000..eb1e087 --- /dev/null +++ b/source/scripts/tests/reader/json.coffee @@ -0,0 +1,33 @@ +test("jsonReader", () -> + injector = angular.injector(["ng", "sideBySide"]) + jsonReader = injector.get("jsonReader") + + testCases = [{ + tested: """ + { + meta: { + Title: "Test leading text" + }, + content: [ + "

This is leading text.

", + { section: "I", text: "

First section.

" }, + { section: "II", text: "

Second section.

" }, + { text: "

Another section.

" }, + ], + }""" + expected: { + meta: { + Title: "Test leading text" + } + content: [ + { section: "", text: "

This is leading text.

" } + { section: "I", text: "

First section.

" } + { section: "II", text: "

Second section.

" } + { section: "", text: "

Another section.

" } + ] + } + }] + + for testCase in testCases + deepEqual(jsonReader(testCase.tested), testCase.expected) +) diff --git a/source/tests.haml b/source/tests.haml index cc78807..8b86676 100644 --- a/source/tests.haml +++ b/source/tests.haml @@ -9,6 +9,7 @@ #{stylesheet_link_tag "//cdnjs.cloudflare.com/ajax/libs/qunit/1.11.0/qunit.css"} #{javascript_include_tag "//cdnjs.cloudflare.com/ajax/libs/qunit/1.11.0/qunit.js"} #{javascript_include_tag "tests/reader/markdown"} + #{javascript_include_tag "tests/reader/json"} %body %div{:id => "qunit"}