Created
October 14, 2024 20:37
-
-
Save XCaminhante/172bf2c30ba3fe146f669ee62d9af88b to your computer and use it in GitHub Desktop.
Pequena biblioteca para produzir protocolo TAP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@+leo-ver=5-thin | |
//@+node:caminhante.20241010170109.1: * @file TAP.h | |
//@@language c | |
#pragma once | |
//@+others | |
//@+node:caminhante.20241010170419.1: ** /includes | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <assert.h> | |
//@+node:caminhante.20241010170422.1: ** /constantes e tipos | |
//@+node:caminhante.20241010170412.1: *3* struct TapPlan | |
struct TapPlan { | |
uint32_t testNumber; | |
uint32_t maxTests; | |
uint8_t yamlIdentation; | |
bool givenUp; | |
}; | |
//@+node:caminhante.20241010170417.1: ** void tapVersion (uint32_t version) | |
void tapVersion (uint32_t version) { | |
assert(version >= 13); | |
printf("TAP version %u\n", version); | |
} | |
//@+node:caminhante.20241010170436.1: ** /Plan | |
//@+node:caminhante.20241010170406.1: *3* void TapPlanStart (struct TapPlan *plan, uint32_t maxTests) | |
void TapPlanStart (struct TapPlan *plan, uint32_t maxTests) { | |
assert(plan->testNumber == 0); | |
assert(maxTests >= 1); | |
plan->maxTests = maxTests; | |
printf("1..%u\n", maxTests); | |
} | |
//@+node:caminhante.20241010170318.1: *3* void TapPlanEnd (struct TapPlan *plan) | |
void TapPlanEnd (struct TapPlan *plan) { | |
if (plan->givenUp) {return;} | |
plan->givenUp = true; | |
if (plan->maxTests > 0) { | |
assert(plan->testNumber <= plan->maxTests); | |
printf("1..%u\n", plan->maxTests); | |
} else { | |
assert(plan->testNumber > 0); | |
printf("1..%u\n", plan->testNumber); | |
} | |
} | |
//@+node:caminhante.20241010170307.1: *3* void TapPlanSkip (struct TapPlan *plan, const char *description) | |
void TapPlanSkip (struct TapPlan *plan, const char *description) { | |
assert(plan->testNumber == 0); | |
if (plan->givenUp) {return;} | |
plan->givenUp = true; | |
printf("1..0 # %s\n", description); | |
} | |
//@+node:caminhante.20241010170303.1: *3* void TapPlanBailOut (struct TapPlan *plan, const char *description) | |
void TapPlanBailOut (struct TapPlan *plan, const char *description) { | |
if (plan->givenUp) {return;} | |
plan->givenUp = true; | |
printf("Bail out! %s\n", description); | |
} | |
//@+node:caminhante.20241010170445.1: ** /Test | |
//@+node:caminhante.20241010170258.1: *3* void TapTest (struct TapPlan *plan, bool expression, const char *description) | |
void TapTest (struct TapPlan *plan, bool expression, const char *description) { | |
if (plan->givenUp) {return;} | |
plan->testNumber += 1; | |
if (plan->maxTests > 0) { assert(plan->testNumber <= plan->maxTests); } | |
printf("%s %u - %s\n", (expression ? "ok" : "not ok"), plan->testNumber, description); | |
} | |
//@+node:caminhante.20241010170252.1: *3* void TapTestSkip (struct TapPlan *plan, const char *description) | |
void TapTestSkip (struct TapPlan *plan, const char *description) { | |
if (plan->givenUp) {return;} | |
plan->testNumber += 1; | |
if (plan->maxTests > 0) { assert(plan->testNumber <= plan->maxTests); } | |
printf("ok %u # SKIP %s\n", plan->testNumber, description); | |
} | |
//@+node:caminhante.20241010170248.1: *3* void TapTestTodo (struct TapPlan *plan, const char *description) | |
void TapTestTodo (struct TapPlan *plan, const char *description) { | |
if (plan->givenUp) {return;} | |
plan->testNumber += 1; | |
if (plan->maxTests > 0) { assert(plan->testNumber <= plan->maxTests); } | |
printf("not ok %u # TODO %s\n", plan->testNumber, description); | |
} | |
//@+node:caminhante.20241010170451.1: ** /Diagnostic | |
//@+node:caminhante.20241010170240.1: *3* void TapDiagnostic (struct TapPlan *plan, const char *text) | |
void TapDiagnostic (struct TapPlan *plan, const char *text) { | |
if (plan->givenUp) {return;} | |
printf("# %s\n", text); | |
} | |
//@+node:caminhante.20241010170207.1: *3* void TapYamlBegin (struct TapPlan *plan) | |
void TapYamlBegin (struct TapPlan *plan) { | |
if (plan->givenUp) {return;} | |
assert(plan->yamlIdentation == 0); | |
printf(" ---\n"); | |
} | |
//@+node:caminhante.20241010170202.1: *3* uint8_t TapYamlLevelUp (struct TapPlan *plan) | |
uint8_t TapYamlLevelUp (struct TapPlan *plan) { | |
if (plan->givenUp) {return 0;} | |
plan->yamlIdentation += 1; | |
return plan->yamlIdentation; | |
} | |
//@+node:caminhante.20241010170157.1: *3* uint8_t TapYamlLevelDown (struct TapPlan *plan) | |
uint8_t TapYamlLevelDown (struct TapPlan *plan) { | |
if (plan->givenUp) {return 0;} | |
assert(plan->yamlIdentation > 0); | |
plan->yamlIdentation -= 1; | |
return plan->yamlIdentation; | |
} | |
//@+node:caminhante.20241010170153.1: *3* void TapYamlIdent (struct TapPlan *plan) | |
void TapYamlIdent (struct TapPlan *plan) { | |
if (plan->givenUp) {return;} | |
for (size_t i = 0; i <= plan->yamlIdentation; i++) { | |
printf(" "); | |
} | |
} | |
//@+node:caminhante.20241010170148.1: *3* void TapYamlName (struct TapPlan *plan, const char *name) | |
void TapYamlName (struct TapPlan *plan, const char *name) { | |
if (plan->givenUp) {return;} | |
TapYamlIdent(plan); | |
printf("%s:\n", name); | |
TapYamlLevelUp(plan); | |
} | |
//@+node:caminhante.20241010170143.1: *3* void TapYamlKeyValue (struct TapPlan *plan, const char *key, const char *value) | |
void TapYamlKeyValue (struct TapPlan *plan, const char *key, const char *value) { | |
if (plan->givenUp) {return;} | |
TapYamlIdent(plan); | |
printf("%s: '%s'\n", key,value); | |
} | |
//@+node:caminhante.20241010170138.1: *3* void TapYamlArrayItem (struct TapPlan *plan, const char *item) | |
void TapYamlArrayItem (struct TapPlan *plan, const char *item) { | |
if (plan->givenUp) {return;} | |
TapYamlIdent(plan); | |
printf("- '%s'\n", item); | |
} | |
//@+node:caminhante.20241010170133.1: *3* void TapYamlEnd (struct TapPlan *plan) | |
void TapYamlEnd (struct TapPlan *plan) { | |
if (plan->givenUp) {return;} | |
plan->yamlIdentation = 0; | |
printf(" ...\n"); | |
} | |
//@-others | |
//@-leo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment