Created
September 25, 2021 18:28
-
-
Save tonsky/e006dcd5964e279ab76b79e34db16a52 to your computer and use it in GitHub Desktop.
Refactoring of piece of code from Clean Code book
This file contains hidden or 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
package fitnesse.html; | |
import fitnesse.responders.run.SuiteResponder; | |
import fitnesse.wiki.*; | |
public class SetupTeardownIncluder { | |
public static String render(PageData pageData, boolean isSuite) throws Exception { | |
StringBuilder sb = new StringBuilder(); | |
WikiPage testPage = pageData.getWikiPage(); | |
if (pageData.hasAttribute("Test")) { | |
if (isSuite) | |
sb.append(include(testPage, SuiteResponder.SUITE_SETUP_NAME, "-setup")); | |
sb.append(include(testPage, "SetUp", "-setup")); | |
sb.append(pageData.getContent()); | |
sb.append(include(testPage, "TearDown", "-teardown")); | |
if (isSuite) | |
sb.append(include(testPage, SuiteResponder.SUITE_TEARDOWN_NAME, "-teardown")); | |
pageData.setContent(sb.toString()); | |
} | |
return pageData.getHtml(); | |
} | |
private static String include(WikiPage testPage, String pageName, String arg) throws Exception { | |
WikiPage inheritedPage = PageCrawlerImpl.getInheritedPage(pageName, testPage); | |
if (inheritedPage != null) { | |
WikiPagePath pagePath = testPage.getPageCrawler().getFullPath(inheritedPage); | |
return "\n!include " + arg + " ." + PathParser.render(pagePath) + "\n"; | |
} else | |
return ""; | |
} | |
} |
This file contains hidden or 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
package fitnesse.html; | |
import fitnesse.responders.run.SuiteResponder; | |
import fitnesse.wiki.*; | |
public class SetupTeardownIncluder { | |
private PageData pageData; | |
private boolean isSuite; | |
private WikiPage testPage; | |
private StringBuffer newPageContent; | |
private PageCrawler pageCrawler; | |
public static String render(PageData pageData) throws Exception { | |
return render(pageData, false); | |
} | |
public static String render(PageData pageData, boolean isSuite) | |
throws Exception { | |
return new SetupTeardownIncluder(pageData).render(isSuite); | |
} | |
private SetupTeardownIncluder(PageData pageData) { | |
this.pageData = pageData; | |
testPage = pageData.getWikiPage(); | |
pageCrawler = testPage.getPageCrawler(); | |
newPageContent = new StringBuffer(); | |
} | |
private String render(boolean isSuite) throws Exception { | |
this.isSuite = isSuite; | |
if (isTestPage()) | |
includeSetupAndTeardownPages(); | |
return pageData.getHtml(); | |
} | |
private boolean isTestPage() throws Exception { | |
return pageData.hasAttribute("Test"); | |
} | |
private void includeSetupAndTeardownPages() throws Exception { | |
includeSetupPages(); | |
includePageContent(); | |
includeTeardownPages(); | |
updatePageContent(); | |
} | |
private void includeSetupPages() throws Exception { | |
if (isSuite) | |
includeSuiteSetupPage(); | |
includeSetupPage(); | |
} | |
private void includeSuiteSetupPage() throws Exception { | |
include(SuiteResponder.SUITE_SETUP_NAME, "-setup"); | |
} | |
private void includeSetupPage() throws Exception { | |
include("SetUp", "-setup"); | |
} | |
private void includePageContent() throws Exception { | |
newPageContent.append(pageData.getContent()); | |
} | |
private void includeTeardownPages() throws Exception { | |
includeTeardownPage(); | |
if (isSuite) | |
includeSuiteTeardownPage(); | |
} | |
private void includeTeardownPage() throws Exception { | |
include("TearDown", "-teardown"); | |
} | |
private void includeSuiteTeardownPage() throws Exception { | |
include(SuiteResponder.SUITE_TEARDOWN_NAME, "-teardown"); | |
} | |
private void updatePageContent() throws Exception { | |
pageData.setContent(newPageContent.toString()); | |
} | |
private void include(String pageName, String arg) throws Exception { | |
WikiPage inheritedPage = findInheritedPage(pageName); | |
if (inheritedPage != null) { | |
String pagePathName = getPathNameForPage(inheritedPage); | |
buildIncludeDirective(pagePathName, arg); | |
} | |
} | |
private WikiPage findInheritedPage(String pageName) throws Exception { | |
return PageCrawlerImpl.getInheritedPage(pageName, testPage); | |
} | |
private String getPathNameForPage(WikiPage page) throws Exception { | |
WikiPagePath pagePath = pageCrawler.getFullPath(page); | |
return PathParser.render(pagePath); | |
} | |
private void buildIncludeDirective(String pagePathName, String arg) { | |
newPageContent | |
.append("\n!include ") | |
.append(arg) | |
.append(" .") | |
.append(pagePathName) | |
.append("\n"); | |
} | |
} |
Welcome to the Go, kid.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
хо-ро-ше-ч-но