Skip to content

Commit

Permalink
Permitindo atribuição de valores String aos modificadores (#162)
Browse files Browse the repository at this point in the history
* Debug do tratamento atual de valores string
* Ajustando Lexador para mapear lexema de valor string devidamente
* Adaptando lógica de modificadores que aceitam valores string
* Testes unitários - atribuindo valores string aos modificadores
* Testes unitários - caso de falha
  • Loading branch information
VitBrandao authored Jan 26, 2025
1 parent 209f897 commit cbf55d9
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 12 deletions.
9 changes: 9 additions & 0 deletions exemplos/exemplo2.foles
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ lmht {
area-mascara: visualizar-caixa, completar-caixa, borda-caixa;
atraso-transicao: 2s, 4ms;
duracao-animacao: 10s, 35s, 230ms;
}

lmht {
citações: "x";
configuracoes-variacao-fonte: "<";
enfase-texto: 'foo';
estilo-enfase-texto: "\25B2";
substituir-idioma-fonte: "«" "»" "‹" "›";
vazamento-texto: 'xxxxx';
}
2 changes: 1 addition & 1 deletion fontes/foles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ export class FolEs {
}

// const a = new FolEs(false);
// console.log(a.converterParaCss('../exemplos/exemplo.foles'));
// console.log(a.converterParaCss('../exemplos/exemplo2.foles'));
// console.log(a.converterParaFolEs('../exemplos/reverso/exemplo-metodos.css'));
// console.log(a.converterParaFolEs('../exemplos/reverso/exemplo-codigo.css'));
10 changes: 9 additions & 1 deletion fontes/lexador/lexador.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,19 @@ export class Lexador implements LexadorInterface {
literal: any = null,
lexema: string = null
): void {
const texto: string = this.codigo[this.linha].substring(
let texto: string;
texto = this.codigo[this.linha].substring(
this.inicioSimbolo,
this.atual
);

if (tipo === tiposDeSimbolos.TEXTO) {
texto = this.codigo[this.linha].substring(
this.inicioSimbolo,
this.atual + 1
);
}

this.simbolos.push(
new Simbolo(
tipo,
Expand Down
4 changes: 4 additions & 0 deletions fontes/modificadores/citacoes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class Citacoes extends Modificador {

constructor(valor: string, quantificador?: string, pragmas?: PragmasModificador) {
super(["citacoes", "citações"], "quotes", pragmas);

if (valor.includes("'") || valor.includes('"')) {
this.valoresAceitos[valor] = valor;
}

validarValores("citações", valor, this.valoresAceitos);

Expand Down
4 changes: 4 additions & 0 deletions fontes/modificadores/configuracoes-variacao-fonte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class ConfiguracoesVariacaoFonte extends Modificador {
pragmas
);

if (valor.includes("'") || valor.includes('"')) {
this.valoresAceitos[valor] = valor;
}

validarValorNumerico('configurações-variação-fonte', valor, this.valoresAceitos);
this.valor = valor;

Expand Down
4 changes: 4 additions & 0 deletions fontes/modificadores/enfase-texto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class EnfaseTexto extends Modificador {
constructor(valor: string, quantificador?: string, pragmas?: PragmasModificador) {
super(["enfase-texto", "ênfase-texto"], "text-emphasis", pragmas);

if (valor.includes("'") || valor.includes('"')) {
this.valoresAceitos[valor] = valor;
}

validarValorCor('ênfase-texto', valor, this.valoresAceitos);

this.valor = valor;
Expand Down
14 changes: 4 additions & 10 deletions fontes/modificadores/estilo-enfase-texto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ export class EstiloEnfaseTexto extends Modificador {
constructor(valor: string, quantificador?: string, pragmas?: PragmasModificador) {
super(
["estilo-enfase-texto", "estilo-ênfase-texto"],
"text-emphasis-style",
"text-emphasis-style",
pragmas
);

// OBS.: O seletor também aceita receber STRINGS, desde que essas contenham UM caractere.

// Ex.1: estilo-ênfase-texto: "x";
// Ex.2: estilo-ênfase-texto: "\25B2"; // ESSE CASO É PERMITIDO.
// Ex.3: estilo-ênfase-texto: "foo"; // ESSE CASO NÃO É PERMITIDO!

// OBS.2: Dada a complexidade do Exemplo 2, uma simples condicional avaliando se a string
// possui 1 caractere não seria suficiente. A própria documentação não é muito clara sobre esse exemplo.
// https://developer.mozilla.org/en-US/docs/Web/CSS/text-emphasis-style
if (valor.includes("'") || valor.includes('"')) {
this.valoresAceitos[valor] = valor;
}

validarValores('estilo-ênfase-texto', valor, this.valoresAceitos);

Expand Down
4 changes: 4 additions & 0 deletions fontes/modificadores/substituir-idioma-fonte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class SubstituirIdiomaFonte extends Modificador {
constructor(valor: string, quantificador?: string, pragmas?: PragmasModificador) {
super("substituir-idioma-fonte", "font-language-override", pragmas);

if (valor.includes("'") || valor.includes('"')) {
this.valoresAceitos[valor] = valor;
}

validarValores('substituir-idioma-fonte', valor, this.valoresAceitos);
this.valor = valor;

Expand Down
4 changes: 4 additions & 0 deletions fontes/modificadores/vazamento-texto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class VazamentoTexto extends Modificador {
constructor(valor: string, quantificador: string, pragmas?: PragmasModificador) {
super("vazamento-texto", "text-overflow", pragmas);

if (valor.includes("'") || valor.includes('"')) {
this.valoresAceitos[valor] = valor;
}

validarValores('vazamento-texto', valor, this.valoresAceitos);

this.valor = valor;
Expand Down
32 changes: 32 additions & 0 deletions testes/listas/valor-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const ValorString: Array<string> = [
'citações',
'citacoes',
'configurações-variação-fonte',
'configuracoes-variacao-fonte',
'ênfase-texto',
'enfase-texto',
'estilo-ênfase-texto',
'estilo-enfase-texto',
'substituir-idioma-fonte',
'vazamento-texto',
];

export const ValorStringAcentuado: Array<string> = [
'citações',
'configurações-variação-fonte',
'ênfase-texto',
'estilo-ênfase-texto',
'substituir-idioma-fonte',
'vazamento-texto',
];

/*
VALORES STRING - Exemplos
"x"
"<"
'foo'
"\25B2"
"«" "»" "‹" "›"
'xxxxx'
*/
90 changes: 90 additions & 0 deletions testes/modificadores/valor-string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { AvaliadorSintatico } from "../../fontes/avaliador-sintatico";
import { Importador } from "../../fontes/importador";
import { AvaliadorSintaticoInterface, ImportadorInterface, LexadorInterface } from "../../fontes/interfaces";
import { Lexador } from "../../fontes/lexador";
import { SeletorModificador } from "../../fontes/modificadores/superclasse";
import tiposDeSimbolos from "../../fontes/tipos-de-simbolos/foles";
import { Serializador } from "../../fontes/serializadores";
import { ValorString, ValorStringAcentuado } from "../listas/valor-string";

describe('Testando Seletores com VALORES STRING', () => {
describe('Testes Unitários', () => {
let lexador: LexadorInterface;
let importador: ImportadorInterface;
let avaliadorSintatico: AvaliadorSintaticoInterface;
let tradutor: Serializador;

beforeEach(() => {
lexador = new Lexador();
importador = new Importador(lexador);
avaliadorSintatico = new AvaliadorSintatico(importador);
tradutor = new Serializador();
});

it('Caso de sucesso - Lexador, Avaliador e Tradutor', () => {
for (let index = 0; index < Object.keys(ValorString).length; index += 1) {

const valoresString = [
"'x'",
'"«" "»" "‹" "›"',
"'foo'",
"'/25B2'",
];

for (let valIndex = 0; valIndex < valoresString.length; valIndex += 1) {
// Lexador
const resultadoLexador = lexador.mapear([
"corpo {",
`${ValorString[index]}: ${valoresString[valIndex]};`,
"}"
]);

if (valIndex !== 1) {
expect(resultadoLexador.simbolos).toHaveLength(7);
} else {
expect(resultadoLexador.simbolos).toHaveLength(10);
}

if (valIndex < 1) {
expect(resultadoLexador.simbolos).toEqual(
expect.arrayContaining([
expect.objectContaining({ tipo: tiposDeSimbolos.TEXTO }),
])
);
} else {
expect(resultadoLexador.simbolos).toEqual(
expect.arrayContaining([
expect.objectContaining({ tipo: tiposDeSimbolos.IDENTIFICADOR }),
])
);
}

// Avaliador Sintático
const resultadoAvaliadorSintatico = avaliadorSintatico.analisar(resultadoLexador.simbolos);
expect(resultadoAvaliadorSintatico[0].modificadores[0].nomeFoles).toContain(ValorString[index]);
expect(resultadoAvaliadorSintatico[0].modificadores[0].valor).toContain(valoresString[valIndex]);

// Tradutor
const resultadoTradutor = tradutor.serializar(resultadoAvaliadorSintatico);
expect(resultadoTradutor).toContain(valoresString[valIndex]);
}
}
});

it('Caso de falha - Avaliador sintático deve retornar erro ', () => {
for (let index = 0; index < Object.keys(ValorStringAcentuado).length; index += 1) {
// Lexador
const resultadoLexador = lexador.mapear([
"corpo {",
`${ValorStringAcentuado[index]}: x;`,
"}"
]);

// Avaliador Sintático não deve aceitar o valor string sem aspas
expect(() => {
avaliadorSintatico.analisar(resultadoLexador.simbolos);
}).toThrowError(`Propriedade '${ValorStringAcentuado[index]}' com valor x inválido`);
}
});
});
});

0 comments on commit cbf55d9

Please sign in to comment.