diff --git a/__test__/converters/xml-mathml-to-mathml/XmlToMathMLAdapter.test.ts b/__test__/converters/xml-mathml-to-mathml/XmlToMathMLAdapter.test.ts index ca0f1dc..023bdad 100755 --- a/__test__/converters/xml-mathml-to-mathml/XmlToMathMLAdapter.test.ts +++ b/__test__/converters/xml-mathml-to-mathml/XmlToMathMLAdapter.test.ts @@ -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 { singleMi, singleMiNoRoot, @@ -8,11 +13,18 @@ import { } from '../../mocks/mathmlStrings'; 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', () => { it('return array with single mathml interface of name math and child mi', () => { const mathmlString = singleMi; - const result = new XmlToMathMLAdapter(mathmlString).convert(); + const result = makeSut().convert(mathmlString); expect(result.length).toBe(1); expect(result[0]).toMatchObject({ @@ -28,7 +40,7 @@ describe('#convert', () => { test('return array with single mathml interface of name math and child mi', () => { const mathmlString = singleMiNoRoot; - const result = new XmlToMathMLAdapter(mathmlString).convert(); + const result = makeSut().convert(mathmlString); expect(result.length).toBe(1); expect(result[0]).toMatchObject({ @@ -44,7 +56,7 @@ describe('#convert', () => { test('return content in a three and keep value on each child', () => { const mathmlString = mrow; - const result = new XmlToMathMLAdapter(mathmlString).convert(); + const result = makeSut().convert(mathmlString); expect(result.length).toBe(1); expect(result[0]).toMatchObject({ @@ -71,7 +83,7 @@ describe('#convert', () => { test('add attributes to children related with name mfenced', () => { const mathmlString = mfencedWithSeparatorAttribute; - const result = new XmlToMathMLAdapter(mathmlString).convert(); + const result = makeSut().convert(mathmlString); expect(result.length).toBe(1); expect(result[0]).toMatchObject({ @@ -98,7 +110,7 @@ describe('#convert', () => { test('add attributes to children related with name mfenced', () => { const mathmlString = mfencedWithBrokenAttribute; - const result = new XmlToMathMLAdapter(mathmlString).convert(); + const result = makeSut().convert(mathmlString); expect(result.length).toBe(1); expect(result[0]).toMatchObject({ diff --git a/src/infra/usecases/xmldom-to-mathml-elements/index.ts b/src/infra/usecases/xmldom-to-mathml-elements/index.ts index 02f6b74..658a4e6 100644 --- a/src/infra/usecases/xmldom-to-mathml-elements/index.ts +++ b/src/infra/usecases/xmldom-to-mathml-elements/index.ts @@ -1 +1,3 @@ export * from './xmldom-to-mathml-element-adapter'; +export * from './error-handler'; +export * from './xmldom-elements-to-mathml-elements-adapter'; diff --git a/src/infra/usecases/xmldom-to-mathml-elements/xmldom-to-mathml-element-adapter.ts b/src/infra/usecases/xmldom-to-mathml-elements/xmldom-to-mathml-element-adapter.ts index 5828e71..ecfea2e 100644 --- a/src/infra/usecases/xmldom-to-mathml-elements/xmldom-to-mathml-element-adapter.ts +++ b/src/infra/usecases/xmldom-to-mathml-elements/xmldom-to-mathml-element-adapter.ts @@ -6,16 +6,14 @@ import { ErrorHandler } from './error-handler'; import { MathMLElement } from '@/data/protocols/mathml-element'; export class XmlToMathMLAdapter { - private _xml: string; + private _xml = ''; private readonly _xmlDOM: DOMParser; private readonly _errorHandler: ErrorHandler; private readonly _elementsConvertor: ElementsToMathMLAdapter; - constructor(xml: string) { - this._xml = this._removeLineBreaks(xml); - - this._elementsConvertor = new ElementsToMathMLAdapter(); - this._errorHandler = new ErrorHandler(); + constructor(elementsConvertor: ElementsToMathMLAdapter, errorHandler: ErrorHandler) { + this._elementsConvertor = elementsConvertor; + this._errorHandler = errorHandler; this._xmlDOM = new xmldom.DOMParser({ 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); } diff --git a/src/main/factories/index.ts b/src/main/factories/index.ts new file mode 100644 index 0000000..6519590 --- /dev/null +++ b/src/main/factories/index.ts @@ -0,0 +1 @@ +export * from './make-to-math-elements-converter'; diff --git a/src/main/factories/make-to-math-elements-converter.ts b/src/main/factories/make-to-math-elements-converter.ts new file mode 100644 index 0000000..38842f1 --- /dev/null +++ b/src/main/factories/make-to-math-elements-converter.ts @@ -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); +}; diff --git a/src/main/mathml-to-latex.ts b/src/main/mathml-to-latex.ts index ff6cef9..8fd2ad8 100644 --- a/src/main/mathml-to-latex.ts +++ b/src/main/mathml-to-latex.ts @@ -1,9 +1,9 @@ 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 { static convert(mathml: string): string { - const mathmlElements = new XmlToMathMLAdapter(mathml).convert(); + const mathmlElements = makeToMathElementsConverter().convert(mathml); const mathmlElementsToLaTeXConverters = mathmlElements.map((mathMLElement) => new MathMLElementToLatexConverterAdapter(mathMLElement).toLatexConverter(), );