diff --git a/docs/guia/avanzado/tests.md b/docs/guia/avanzado/tests.md index f851c87..1615726 100644 --- a/docs/guia/avanzado/tests.md +++ b/docs/guia/avanzado/tests.md @@ -18,6 +18,16 @@ unitarios, desaconsejamos continuar con esta guía. ## Configuración del template +### Instalar CSpec + +Para instalar CSpec, ejecutá en una terminal los siguientes comandos: + +```bash +git clone https://github.com/mumuki/cspec.git +cd cspec +make +sudo make install +``` ### Excluir archivos fuente En caso de que estemos testeando un módulo con una función `main()`, @@ -50,15 +60,53 @@ sí van a estar disponibles para ser testeadas. ### Agregar unit tests -Todas las test suites se guardan dentro de la carpeta `tests/`. El template ya -incluye una suite a modo de ejemplo: +Por último, para agregar tests a cualquier proyecto (ya sea ejecutable o +biblioteca), debemos hacerlo creando una carpeta `tests` a la misma altura que +`src`: + +```bash +mkdir tests +``` + +Por ejemplo, partiendo del [proyecto base](../), podemos agregar un test inicial: + +```bash +touch tests/example_test.c +``` ``` . +├── makefile +├── settings.mk +├── src +│ └── main.c └── tests └── example_test.c ``` + +Con el siguiente código: + +::: code-group + +```c [example_test.c] +#include +#include +#include + +context (example) { + bool la_verdad = true; + + describe("Hello world") { + it("la_verdad should be true") { + should_bool(la_verdad) be equal to(true); + } end + } end + +} +``` +::: + ::: tip Es una buena práctica replicar la estructura de archivos que tenemos en `src/` diff --git a/docs/guia/index.md b/docs/guia/index.md index 148b01c..3e8502f 100644 --- a/docs/guia/index.md +++ b/docs/guia/index.md @@ -4,23 +4,6 @@ import { repository, version } from '../../package.json' # Primeros pasos -## Dependencias - -Antes de empezar a utilizar los templates, necesitaremos instalar un framework -de Unit Testing llamado [CSpec](https://github.com/mumuki/cspec). - -Para instalarlo, ejecutá en una terminal los siguientes comandos: - -```bash -git clone https://github.com/mumuki/cspec.git -cd cspec -make -sudo make install -``` - -Esto tenés que hacerlo una sola vez por máquina. Si ya lo tenés -instalado, no hace falta volver a ejecutar estos comandos. :smile: - ## Inicialización Primero, vamos a descargar el template en una carpeta con el nombre del @@ -44,10 +27,8 @@ tree . . ├── makefile ├── settings.mk -├── src -│ └── main.c -└── tests - └── example_test.c +└── src + └── main.c ``` Una breve explicación de los archivos que hay en ella: @@ -58,8 +39,6 @@ Una breve explicación de los archivos que hay en ella: | `./settings.mk` | Configuración extra del proyecto (aprenderemos más a lo largo de la guía) | | `./src/` | Carpeta en donde vamos a dejar todos los archivos fuente (.c y .h) del proyecto | | `./src/main.c` | Archivo fuente en donde se encuentra la función `main` del proyecto | -| `./tests/` | Acá vamos a dejar todos unit tests del proyecto (más adelante veremos cómo hacerlo) | -| `./tests/example_test.c` | Un unit test a modo de ejemplo, podemos ignorarlo por ahora | ## Compilación @@ -78,8 +57,7 @@ tree bin obj ``` . ├── bin -│ ├── {nombre-del-proyecto}.out -│ └── {nombre-del-proyecto}_tests.out +│ └── {nombre-del-proyecto}.out └── obj └── main.o ``` @@ -87,7 +65,7 @@ tree bin obj | Directorio | Descripción | | ---------- | ------------------------------------------------------------------------------------------------------------------------------- | | `./obj/` | Para cada archivo fuente en `src` se va a generar un objeto en código assembly con fines de acelerar el proceso de compilación. | -| `./bin/` | El compilador guarda aquí el ejecutable final y el ejecutable de los unit tests. | +| `./bin/` | El compilador guarda aquí el ejecutable final y el ejecutable de los unit tests (en caso de haber). | ::: tip @@ -107,7 +85,6 @@ $ ls -l bin src bin: total 20 -rwxrwxr-x 1 utnso utnso 17256 Mar 12 11:43 ejemplo.out --rwxrwxr-x 1 utnso utnso 17256 Mar 12 11:43 ejemplo_tests.out src: total 4 diff --git a/src/compilation.mk b/src/compilation.mk index 7c308c8..a780899 100644 --- a/src/compilation.mk +++ b/src/compilation.mk @@ -1,11 +1,20 @@ +# Check if tests folder exists +ifneq ($(wildcard tests/*),) +TESTS_ENABLED=1 +endif + # Set prerrequisites SRCS_C += $(shell find src/ -iname "*.c") SRCS_H += $(shell find src/ -iname "*.h") -TESTS_C += $(shell find tests/ -iname "*.c") -TESTS_H += $(shell find tests/ -iname "*.h") DEPS = $(foreach SHL,$(SHARED_LIBPATHS),$(SHL:%=%/bin/lib$(notdir $(SHL)).so)) \ $(foreach STL,$(STATIC_LIBPATHS),$(STL:%=%/bin/lib$(notdir $(STL)).a)) +# Set test prerrequisites +ifeq ($(TESTS_ENABLED),1) +TESTS_C += $(shell find tests/ -iname "*.c") +TESTS_H += $(shell find tests/ -iname "*.h") +endif + # Set header paths to (-I)nclude IDIRS += $(addsuffix /src,$(SHARED_LIBPATHS) $(STATIC_LIBPATHS) .) @@ -17,11 +26,19 @@ RUNDIRS = $(SHARED_LIBPATHS:%=$(shell cd . && pwd)/%/bin) # Set intermediate objects OBJS = $(patsubst src/%.c,obj/%.o,$(SRCS_C)) + +# Set test intermediate objects +ifeq ($(TESTS_ENABLED),1) TEST_OBJS = $(TESTS_C) $(filter-out $(TEST_EXCLUDE), $(SRCS_C)) +endif # Set binary targets BIN = bin/$(call filename,$(shell cd . && pwd | xargs basename)) + +# Set test binary targets +ifeq ($(TESTS_ENABLED),1) TEST = bin/$(shell cd . && pwd | xargs basename)_tests.out +endif .PHONY: all all: CFLAGS = $(CDEBUG) @@ -48,8 +65,10 @@ $(BIN): $(OBJS) | $(dir $(BIN)) obj/%.o: src/%.c $(SRCS_H) $(DEPS) | $(dir $(OBJS)) $(call compile_objs) +ifeq ($(TESTS_ENABLED),1) $(TEST): $(TEST_OBJS) | $(dir $(TEST)) gcc $(CFLAGS) -o "$@" $^ $(IDIRS:%=-I%) $(LIBDIRS:%=-L%) $(RUNDIRS:%=-Wl,-rpath,%) $(LIBS:%=-l%) -lcspecs +endif .SECONDEXPANSION: $(DEPS): $$(shell find $$(patsubst %bin/,%src/,$$(dir $$@)) -iname "*.c") \ diff --git a/src/project/tests/example_test.c b/src/project/tests/example_test.c deleted file mode 100644 index b60395f..0000000 --- a/src/project/tests/example_test.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include - -context (example) { - bool la_verdad = true; - - describe("Hello world") { - it("la_verdad should be true") { - should_bool(la_verdad) be equal to(true); - } end - } end - -} diff --git a/src/shared/tests/example_test.c b/src/shared/tests/example_test.c deleted file mode 100644 index b60395f..0000000 --- a/src/shared/tests/example_test.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include - -context (example) { - bool la_verdad = true; - - describe("Hello world") { - it("la_verdad should be true") { - should_bool(la_verdad) be equal to(true); - } end - } end - -} diff --git a/src/static/tests/example_test.c b/src/static/tests/example_test.c deleted file mode 100644 index b60395f..0000000 --- a/src/static/tests/example_test.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include - -context (example) { - bool la_verdad = true; - - describe("Hello world") { - it("la_verdad should be true") { - should_bool(la_verdad) be equal to(true); - } end - } end - -} diff --git a/src/testing.mk b/src/testing.mk index 9f11f31..56ef1c5 100644 --- a/src/testing.mk +++ b/src/testing.mk @@ -1,3 +1,4 @@ +ifeq ($(TESTS_ENABLED),1) .PHONY: test test: all valgrind --tool=none ./$(TEST) @@ -16,3 +17,4 @@ test-memcheck: all .PHONY: test-helgrind test-helgrind: all valgrind --tool=helgrind $(HELGRIND_FLAGS) ./$(TEST) +endif