The xml parse works for <yes/>. Adding tests

This commit is contained in:
Tristan Roux
2019-12-05 20:34:59 +01:00
parent 76b81478e5
commit 6291a21f71
9 changed files with 85 additions and 9 deletions

View File

@@ -11,11 +11,17 @@ SRC = src/xmlparser.c \
OBJ = $(SRC:%.c=%.o) OBJ = $(SRC:%.c=%.o)
TESTS = tests/test_basics.c
TEST_MAIN = tests/test_main.c
COVERAGE = --coverage -lcriterion
INCLUDE = -I ./include INCLUDE = -I ./include
CFLAGS = $(INCLUDE) -Wall -Wextra -Wshadow CFLAGS = $(INCLUDE) -Wall -Wextra -Wshadow
LDFLAGS = LDFLAGS = -lmy -L lib/my/
CC = gcc CC = gcc
@@ -23,20 +29,37 @@ AR = ar rc
NAME = xmlparser.a NAME = xmlparser.a
UT = ./ut
FT = ./ft
all: build all: build
build: $(OBJ) build: $(OBJ)
$(MAKE) -C lib/my
$(AR) $(NAME) $(NAME) $(AR) $(NAME) $(NAME)
tests_run:
$(CC) -o $(UT) $(SRC) $(TESTS) $(COVERAGE) $(CFLAGS) $(LDFLAGS)
$(UT)
clean: clean:
$(MAKE) -C lib/my clean
$(RM) $(OBJ) $(RM) $(OBJ)
$(RM) *.gc*
fclean: clean fclean: clean
$(MAKE) -C lib/my fclean
$(RM) $(NAME) $(RM) $(NAME)
$(RM) $(UT)
$(RM) $(FT)
re: fclean all re: fclean all
dbg: CFLAGS += -g dbg: CFLAGS += -g
dbg: re dbg: fclean
dbg: $(OBJ)
$(MAKE) -C lib/my
$(CC) -o $(FT) $(SRC) $(TEST_MAIN) $(CFLAGS) $(LDFLAGS)
.PHONY: all build clean fclean .PHONY: all build clean fclean

View File

@@ -8,6 +8,8 @@
int count_valid_queens_placements(int n); int count_valid_queens_placements(int n);
char *my_strchr(char *str, char c);
int my_compute_power_it(int n, int p); int my_compute_power_it(int n, int p);
int my_compute_power_rec(int n, int p); int my_compute_power_rec(int n, int p);
@@ -74,9 +76,7 @@ char *my_strcat(char *dest, const char *src);
int my_strcmp(const char *s1, const char *s2); int my_strcmp(const char *s1, const char *s2);
char *my_strchr(char *str, char c); char *my_strcpy(const char *str);
char *my_strcpy(char *dest, const char *src);
int my_str_isalpha(const char *str); int my_str_isalpha(const char *str);

View File

@@ -27,5 +27,6 @@ struct node
node *next; node *next;
}; };
node *xmlparse(char *path);
dictionary *property_add(dictionary *list, dictionary *property); dictionary *property_add(dictionary *list, dictionary *property);
node *xml_parsenode(char **nodestr); node *xml_parsenode(char **nodestr);

View File

@@ -17,7 +17,6 @@ LIBOBJ = lib/my/*.o \
all: buildlib all: buildlib
buildlib: buildlib:
cp ./my.h ../../include/my.h
gcc -c ./*.c gcc -c ./*.c
ar rc ./libmy.a ./*.o ar rc ./libmy.a ./*.o
ar rc ../libmy.a ./*.o ar rc ../libmy.a ./*.o

View File

@@ -8,6 +8,8 @@
int count_valid_queens_placements(int n); int count_valid_queens_placements(int n);
char *my_strchr(char *str, char c);
int my_compute_power_it(int n, int p); int my_compute_power_it(int n, int p);
int my_compute_power_rec(int n, int p); int my_compute_power_rec(int n, int p);

View File

@@ -17,11 +17,14 @@ char *xml_getname(char **nodestr, bool *has_parameters)
if ((*nodestr)[0] != '<') if ((*nodestr)[0] != '<')
return (NULL); return (NULL);
*nodestr += 1;
if (p) { if (p) {
*has_parameters = true; *has_parameters = true;
} else { } else {
*has_parameters = false; *has_parameters = false;
p = my_strchr(*nodestr, '>'); p = my_strchr(*nodestr, '/');
if (!p)
p = my_strchr(*nodestr, '>');
if (!p) if (!p)
return (NULL); return (NULL);
} }
@@ -70,16 +73,24 @@ dictionary *xml_getproperties(char **nodestr, bool *can_have_child)
*nodestr += 1; *nodestr += 1;
if ((*nodestr)[0] == '/') { if ((*nodestr)[0] == '/') {
*nodestr += 1; *nodestr += 1;
*can_have_child = true; *can_have_child = false;
} }
else else
*can_have_child = false; *can_have_child = true;
} }
return (properties); return (properties);
} }
int xml_checkclosing(node *n, char **nodestr) int xml_checkclosing(node *n, char **nodestr)
{ {
if ((*nodestr)[0] == '/' && (*nodestr)[1] == '>') {
*nodestr += 2;
return (0);
}
if ((*nodestr)[0] == '>') {
*nodestr += 1;
return (0);
}
if ((*nodestr)[0] != '<' || (*nodestr)[1] != '/') if ((*nodestr)[0] != '<' || (*nodestr)[1] != '/')
return (-1); return (-1);
if (my_strstr(*nodestr, n->name) != *nodestr + 2) if (my_strstr(*nodestr, n->name) != *nodestr + 2)

21
tests/test_basics.c Normal file
View File

@@ -0,0 +1,21 @@
/*
** EPITECH PROJECT, 2019
** xmlparser
** File description:
** test_basics
*/
#include "xml.h"
#include <criterion/criterion.h>
#include <string.h>
Test(xml, simple)
{
char *xml = strdup("<yes/>");
node *n = xml_parsenode(&xml);
cr_assert_str_eq(n->name, "yes");
cr_assert_eq(n->child, NULL);
cr_assert_eq(n->next, NULL);
cr_assert_eq(n->properties, NULL);
}

18
tests/test_main.c Normal file
View File

@@ -0,0 +1,18 @@
/*
** EPITECH PROJECT, 2019
** xmlparser
** File description:
** test_main
*/
#include "xml.h"
#include <stdio.h>
int main(int argc, char **argv)
{
if (argc != 2)
return (printf("Usage: %s xml_path\n", argv[0]), 0);
node *n = xmlparse(argv[1]);
(void)n;
return (0);
}

1
tests/xmltest.txt Normal file
View File

@@ -0,0 +1 @@
<yes/>