diff --git a/src/lib/c/factories.c b/src/lib/c/factories.c index b9d9970..32c79ab 100644 --- a/src/lib/c/factories.c +++ b/src/lib/c/factories.c @@ -83,6 +83,9 @@ SourceFormatDetection detectSourceFormat(const RcnSourceFile* file) { || strcmp(extension, "markdown") == 0) { detection.isSupportedFormat = true; detection.format = RCN_TEXT_MARKDOWN; + } else if (strcmp(extension, "json") == 0) { + detection.isSupportedFormat = true; + detection.format = RCN_TEXT_JSON; } else if (strcmp(extension, "txt") == 0) { detection.isSupportedFormat = true; detection.format = RCN_TEXT_UNFORMATTED; diff --git a/src/lib/include/reckon/reckon.h b/src/lib/include/reckon/reckon.h index a282c50..d45fd57 100644 --- a/src/lib/include/reckon/reckon.h +++ b/src/lib/include/reckon/reckon.h @@ -100,7 +100,7 @@ extern "C" { * The total number of supported text formats, including * supported programming languages. */ -#define RECKON_NUM_SUPPORTED_FORMATS 4 +#define RECKON_NUM_SUPPORTED_FORMATS 5 /** * Macro to create a format option bitmask. @@ -145,15 +145,20 @@ typedef enum RcnTextFormat { */ RCN_TEXT_MARKDOWN = 1, + /** + * Text formatted in JSON, as found in files with a '.json' extension. + */ + RCN_TEXT_JSON = 2, + /** * Source files for the C programming language. */ - RCN_LANG_C = 2, + RCN_LANG_C = 3, /** * Source files for the Java programming language. */ - RCN_LANG_JAVA = 3 + RCN_LANG_JAVA = 4 } RcnTextFormat; @@ -691,6 +696,11 @@ typedef enum RcnFormatOption { */ RCN_OPT_TEXT_MARKDOWN = RECKON_MK_FRMT_OPT(RCN_TEXT_MARKDOWN), + /** + * Option to select statistics for JSON text files. + */ + RCN_OPT_TEXT_JSON = RECKON_MK_FRMT_OPT(RCN_TEXT_JSON), + /** * Option to select statistics for source code files written in * the C programming language. diff --git a/src/lib/tests/res/misc/sample.json b/src/lib/tests/res/misc/sample.json new file mode 100644 index 0000000..b511de3 --- /dev/null +++ b/src/lib/tests/res/misc/sample.json @@ -0,0 +1,37 @@ +{ + "meta": { + "name": "sample", + "version": 1, + "generatedAt": "1970-01-01T00:00:00Z", + "tags": [ + "test", + "fixture", + "json" + ] + }, + "user": { + "id": 12345, + "username": "bla", + "active": true + }, + "items": [ + { + "id": "itm_001", + "name": "Widget", + "qty": 2, + "attributes": { + "color": "blue", + "size": "M" + } + }, + { + "id": "itm_002", + "name": "Gadget", + "qty": 1, + "attributes": { + "color": "red", + "size": "S" + } + } + ] +} diff --git a/src/lib/tests/unit/c/test_statistics.c b/src/lib/tests/unit/c/test_statistics.c index 425ba3d..f2a8c77 100644 --- a/src/lib/tests/unit/c/test_statistics.c +++ b/src/lib/tests/unit/c/test_statistics.c @@ -244,6 +244,31 @@ void testCountResultGroupLogicalLineCheckField(void) { rcnFreeCountStatistics(stats); } +void testCountResultsJson(void) { + char* path = RECKON_TEST_PATH_RES_BASE "/misc/sample.json"; + RcnCountStatistics* stats = rcnCreateCountStatistics(path); + RcnStatOptions options = { + .formats = RCN_OPT_TEXT_JSON + }; + + rcnCount(stats, options); + + TEST_ASSERT_EQUAL_INT(1, stats->count.size); + RcnSourceFile* file = &stats->count.files[0]; + RcnCountResultGroup* result = &stats->count.results[0]; + TEST_ASSERT_TRUE(result->state.ok); + TEST_ASSERT_EQUAL_INT(RCN_ERR_NONE, result->state.errorCode); + TEST_ASSERT_NULL(result->state.errorMessage); + TEST_ASSERT_EQUAL_STRING("sample.json", file->name); + TEST_ASSERT_TRUE(result->isProcessed); + TEST_ASSERT_EQUAL_INT(0, result->logicalLines); + TEST_ASSERT_EQUAL_INT(37, result->physicalLines); + TEST_ASSERT_EQUAL_INT(59, result->words); + TEST_ASSERT_EQUAL_INT(561, result->characters); + TEST_ASSERT_EQUAL_INT(561, result->sourceSize); + rcnFreeCountStatistics(stats); +} + // NOLINTEND(readability-magic-numbers) int main(void) { @@ -255,5 +280,6 @@ int main(void) { RUN_TEST(testCountWhenFileHasUnsupportedFormat); RUN_TEST(testCountWithMultipleFilesWhenOneFileHasError); RUN_TEST(testCountResultGroupLogicalLineCheckField); + RUN_TEST(testCountResultsJson); return UNITY_END(); } diff --git a/src/scount/c/print.c b/src/scount/c/print.c index 7bc9993..31aa77d 100644 --- a/src/scount/c/print.c +++ b/src/scount/c/print.c @@ -480,6 +480,9 @@ static void prSummaryRows( case RCN_TEXT_MARKDOWN: label = "Markdown"; break; + case RCN_TEXT_JSON: + label = "JSON"; + break; case RCN_LANG_C: label = "C"; hasLogicalLines = true; diff --git a/src/scount/tests/functionality/res/expected/mixed.txt b/src/scount/tests/functionality/res/expected/mixed.txt index c418eec..18c4af0 100644 --- a/src/scount/tests/functionality/res/expected/mixed.txt +++ b/src/scount/tests/functionality/res/expected/mixed.txt @@ -1,13 +1,15 @@ Directory: mixed -Scanned files: 8 +Scanned files: 10 o---------- File ----------o--- LLC ---o--- PHL ---o--- WRD ---o--- CHR ---o--- SZE ---o | Sample1.java | 3 | 12 | 34 | 233 | 233 | | Sample2.java | 4 | 13 | 38 | 286 | 286 | | sample1.c | 4 | 10 | 29 | 180 | 180 | + | sample1.json | n/a | 19 | 29 | 250 | 250 | | sample1.md | n/a | 1 | 8 | 52 | 52 | | sample1.txt | n/a | 1 | 9 | 52 | 52 | | sample2.c | 5 | 11 | 33 | 219 | 219 | + | sample2.json | n/a | 23 | 35 | 311 | 311 | | sample2.md | n/a | 1 | 8 | 53 | 53 | | sample2.txt | n/a | 2 | 13 | 75 | 75 | o--------------------------o-----------o-----------o-----------o-----------o-----------o @@ -17,10 +19,11 @@ Summary: o-------- Language --------o--- LLC ---o--- PHL ---o--- WRD ---o--- CHR ---o--- SZE ---o | Plain Text | n/a | 3 | 22 | 127 | 127 | | Markdown | n/a | 2 | 16 | 105 | 105 | + | JSON | n/a | 42 | 64 | 561 | 561 | | C | 9 | 21 | 62 | 399 | 399 | | Java | 7 | 25 | 72 | 519 | 519 | o==========================o===========o===========o===========o===========o===========o - | Total: | 16 | 51 | 172 | 1150 | 1150 | + | Total: | 16 | 93 | 236 | 1711 | 1711 | o==========================o===========o===========o===========o===========o===========o diff --git a/src/scount/tests/functionality/res/mixed/sample1.json b/src/scount/tests/functionality/res/mixed/sample1.json new file mode 100644 index 0000000..01c1f54 --- /dev/null +++ b/src/scount/tests/functionality/res/mixed/sample1.json @@ -0,0 +1,19 @@ +{ + "sample": { + "title": "First Sample", + "items": [ + { + "id": "1", + "value": "One" + }, + { + "id": "2", + "value": "Two" + }, + { + "id": "3", + "value": "Three" + } + ] + } +} diff --git a/src/scount/tests/functionality/res/mixed/sample2.json b/src/scount/tests/functionality/res/mixed/sample2.json new file mode 100644 index 0000000..5eb0466 --- /dev/null +++ b/src/scount/tests/functionality/res/mixed/sample2.json @@ -0,0 +1,23 @@ +{ + "sample": { + "title": "Second Sample", + "items": [ + { + "id": "1", + "value": "One" + }, + { + "id": "2", + "value": "Two" + }, + { + "id": "3", + "value": "Three" + }, + { + "id": "4", + "value": "Four" + } + ] + } +}