Skip to content

Commit

Permalink
test(str): use json files
Browse files Browse the repository at this point in the history
  • Loading branch information
RiadhAdrani committed Jan 21, 2025
1 parent b3554a3 commit 58ff64b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions tests/json/str_escaped_forbidden_chars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"h\ne\tl\\l\bo\fw\rorld"
1 change: 1 addition & 0 deletions tests/json/str_no_ending_quote.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"hello world
2 changes: 2 additions & 0 deletions tests/json/str_no_new_line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"
"
1 change: 1 addition & 0 deletions tests/json/str_no_starting_quote.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world"
1 change: 1 addition & 0 deletions tests/json/str_simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"hello world"
1 change: 1 addition & 0 deletions tests/json/str_stop_on_quote.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"hello world"; 123
52 changes: 36 additions & 16 deletions tests/str.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../str.h"
Expand All @@ -7,90 +8,109 @@
void test_no_empty_string_str()
{
int index = 0;
char *json = get_json_file_content("tests/json/empty.json");
int len = strlen(json);

JsonValue *res = match_string(json, &index, len);

char json[] = "";
JsonValue *res = match_string(json, &index, 0);
CU_ASSERT_EQUAL(res->end, 0);
CU_ASSERT_EQUAL(res->start, 0);
CU_ASSERT_EQUAL(res->error, 1);
CU_ASSERT_EQUAL(res->type, JSON_STRING);

free_json_value(res);
free(json);
}

void test_simple_str()
{
int index = 0;
char *json = get_json_file_content("tests/json/str_simple.json");
int len = strlen(json);

char json[] = "\"hello world\"";
JsonValue *res = match_string(json, &index, 13);
CU_ASSERT_EQUAL(res->end, 12);
JsonValue *res = match_string(json, &index, len);

CU_ASSERT_EQUAL(res->end, len - 2);
CU_ASSERT_EQUAL(res->error, 0);
CU_ASSERT_EQUAL(res->type, JSON_STRING);

free_json_value(res);
free(json);
}

void test_stop_before_end_str()
{
int index = 0;
char *json = get_json_file_content("tests/json/str_stop_on_quote.json");
int len = strlen(json);

JsonValue *res = match_string(json, &index, len);

char json[] = "\"hello world\";123";
JsonValue *res = match_string(json, &index, 17);
CU_ASSERT_EQUAL(res->end, 12);
CU_ASSERT_EQUAL(res->error, 0);

free_json_value(res);
free(json);
}

void test_missing_starting_double_quote()
{
int index = 0;
char *json = get_json_file_content("tests/json/str_no_starting_quote.json");
int len = strlen(json);

JsonValue *res = match_string(json, &index, len);

char json[] = "hello world\"";
JsonValue *res = match_string(json, &index, 12);
CU_ASSERT_EQUAL(res->end, 0);
CU_ASSERT_EQUAL(res->error, 1);

free_json_value(res);
free(json);
}

void test_missing_ending_double_quote()
{
int index = 0;
char *json = get_json_file_content("tests/json/str_no_ending_quote.json");
int len = strlen(json);

JsonValue *res = match_string(json, &index, len);

char json[] = "\"hello world";
JsonValue *res = match_string(json, &index, 12);
CU_ASSERT_EQUAL(res->end, 12);
CU_ASSERT_EQUAL(res->error, 1);

free_json_value(res);
free(json);
}

void test_no_forbidden_chars()
{
int index = 0;
char *json = get_json_file_content("tests/json/str_no_new_line.json");
int len = strlen(json);

char *json = "\"\n\"";
JsonValue *res = match_string(json, &index, len);

JsonValue *res = match_string(json, &index, 3);
CU_ASSERT_EQUAL(res->end, 1);
CU_ASSERT_EQUAL(res->error, 1);

free_json_value(res);
free(json);
}

void test_escaped_forbidden_chars()
{
char *json = "\"h\\be\\fl\\nl\\ro\\t\"";
int len = strlen(json);
int index = 0;
char *json = get_json_file_content("tests/json/str_escaped_forbidden_chars.json");
int len = strlen(json);

JsonValue *res = match_string(json, &index, len);
CU_ASSERT_EQUAL(res->end, len - 1);

CU_ASSERT_EQUAL(res->end, len - 2);
CU_ASSERT_EQUAL(res->error, 0);

free_json_value(res);
free(json);
}

CU_TestInfo str_tests[] = {
Expand Down

0 comments on commit 58ff64b

Please sign in to comment.