refactor: expose dependencies of XmlToMathMLAdapter

This commit is contained in:
Alexandre Nunes 2020-10-02 09:39:12 -03:00
parent abee69ff21
commit 8cd62236db
6 changed files with 37 additions and 15 deletions

View File

@ -1,4 +1,9 @@
import { XmlToMathMLAdapter } from '../../../src/infra/usecases/xmldom-to-mathml-elements'; import {
XmlToMathMLAdapter,
ElementsToMathMLAdapter,
ErrorHandler,
} from '../../../src/infra/usecases/xmldom-to-mathml-elements';
import { import {
singleMi, singleMi,
singleMiNoRoot, singleMiNoRoot,
@ -8,11 +13,18 @@ import {
} from '../../mocks/mathmlStrings'; } from '../../mocks/mathmlStrings';
describe('#convert', () => { describe('#convert', () => {
const makeSut = (): XmlToMathMLAdapter => {
const elementsToMathMLAdapter = new ElementsToMathMLAdapter();
const errorHandler = new ErrorHandler();
return new XmlToMathMLAdapter(elementsToMathMLAdapter, errorHandler);
};
describe('given math string with mi tag', () => { describe('given math string with mi tag', () => {
it('return array with single mathml interface of name math and child mi', () => { it('return array with single mathml interface of name math and child mi', () => {
const mathmlString = singleMi; const mathmlString = singleMi;
const result = new XmlToMathMLAdapter(mathmlString).convert(); const result = makeSut().convert(mathmlString);
expect(result.length).toBe(1); expect(result.length).toBe(1);
expect(result[0]).toMatchObject({ expect(result[0]).toMatchObject({
@ -28,7 +40,7 @@ describe('#convert', () => {
test('return array with single mathml interface of name math and child mi', () => { test('return array with single mathml interface of name math and child mi', () => {
const mathmlString = singleMiNoRoot; const mathmlString = singleMiNoRoot;
const result = new XmlToMathMLAdapter(mathmlString).convert(); const result = makeSut().convert(mathmlString);
expect(result.length).toBe(1); expect(result.length).toBe(1);
expect(result[0]).toMatchObject({ expect(result[0]).toMatchObject({
@ -44,7 +56,7 @@ describe('#convert', () => {
test('return content in a three and keep value on each child', () => { test('return content in a three and keep value on each child', () => {
const mathmlString = mrow; const mathmlString = mrow;
const result = new XmlToMathMLAdapter(mathmlString).convert(); const result = makeSut().convert(mathmlString);
expect(result.length).toBe(1); expect(result.length).toBe(1);
expect(result[0]).toMatchObject({ expect(result[0]).toMatchObject({
@ -71,7 +83,7 @@ describe('#convert', () => {
test('add attributes to children related with name mfenced', () => { test('add attributes to children related with name mfenced', () => {
const mathmlString = mfencedWithSeparatorAttribute; const mathmlString = mfencedWithSeparatorAttribute;
const result = new XmlToMathMLAdapter(mathmlString).convert(); const result = makeSut().convert(mathmlString);
expect(result.length).toBe(1); expect(result.length).toBe(1);
expect(result[0]).toMatchObject({ expect(result[0]).toMatchObject({
@ -98,7 +110,7 @@ describe('#convert', () => {
test('add attributes to children related with name mfenced', () => { test('add attributes to children related with name mfenced', () => {
const mathmlString = mfencedWithBrokenAttribute; const mathmlString = mfencedWithBrokenAttribute;
const result = new XmlToMathMLAdapter(mathmlString).convert(); const result = makeSut().convert(mathmlString);
expect(result.length).toBe(1); expect(result.length).toBe(1);
expect(result[0]).toMatchObject({ expect(result[0]).toMatchObject({

View File

@ -1 +1,3 @@
export * from './xmldom-to-mathml-element-adapter'; export * from './xmldom-to-mathml-element-adapter';
export * from './error-handler';
export * from './xmldom-elements-to-mathml-elements-adapter';

View File

@ -6,16 +6,14 @@ import { ErrorHandler } from './error-handler';
import { MathMLElement } from '@/data/protocols/mathml-element'; import { MathMLElement } from '@/data/protocols/mathml-element';
export class XmlToMathMLAdapter { export class XmlToMathMLAdapter {
private _xml: string; private _xml = '';
private readonly _xmlDOM: DOMParser; private readonly _xmlDOM: DOMParser;
private readonly _errorHandler: ErrorHandler; private readonly _errorHandler: ErrorHandler;
private readonly _elementsConvertor: ElementsToMathMLAdapter; private readonly _elementsConvertor: ElementsToMathMLAdapter;
constructor(xml: string) { constructor(elementsConvertor: ElementsToMathMLAdapter, errorHandler: ErrorHandler) {
this._xml = this._removeLineBreaks(xml); this._elementsConvertor = elementsConvertor;
this._errorHandler = errorHandler;
this._elementsConvertor = new ElementsToMathMLAdapter();
this._errorHandler = new ErrorHandler();
this._xmlDOM = new xmldom.DOMParser({ this._xmlDOM = new xmldom.DOMParser({
locator: this._errorHandler.errorLocator, locator: this._errorHandler.errorLocator,
@ -23,7 +21,8 @@ export class XmlToMathMLAdapter {
}); });
} }
convert(): MathMLElement[] { convert(xml: string): MathMLElement[] {
this._xml = this._removeLineBreaks(xml);
return this._elementsConvertor.convert(this._mathMLElements); return this._elementsConvertor.convert(this._mathMLElements);
} }

View File

@ -0,0 +1 @@
export * from './make-to-math-elements-converter';

View File

@ -0,0 +1,8 @@
import { XmlToMathMLAdapter, ElementsToMathMLAdapter, ErrorHandler } from '@/infra/usecases/xmldom-to-mathml-elements';
export const makeToMathElementsConverter = (): XmlToMathMLAdapter => {
const elementsToMathMLAdapter = new ElementsToMathMLAdapter();
const errorHandler = new ErrorHandler();
return new XmlToMathMLAdapter(elementsToMathMLAdapter, errorHandler);
};

View File

@ -1,9 +1,9 @@
import { MathMLElementToLatexConverterAdapter } from '@/data/usecases/mathml-to-latex-convertion/mathml-element-to-latex-converter-adapter'; import { MathMLElementToLatexConverterAdapter } from '@/data/usecases/mathml-to-latex-convertion/mathml-element-to-latex-converter-adapter';
import { XmlToMathMLAdapter } from '@/infra/usecases/xmldom-to-mathml-elements'; import { makeToMathElementsConverter } from './factories';
export class MathMLToLaTeX { export class MathMLToLaTeX {
static convert(mathml: string): string { static convert(mathml: string): string {
const mathmlElements = new XmlToMathMLAdapter(mathml).convert(); const mathmlElements = makeToMathElementsConverter().convert(mathml);
const mathmlElementsToLaTeXConverters = mathmlElements.map((mathMLElement) => const mathmlElementsToLaTeXConverters = mathmlElements.map((mathMLElement) =>
new MathMLElementToLatexConverterAdapter(mathMLElement).toLatexConverter(), new MathMLElementToLatexConverterAdapter(mathMLElement).toLatexConverter(),
); );