Skip to content

Conversation

@yechoi42
Copy link
Member

@yechoi42 yechoi42 commented May 31, 2021

TDD 방식을 처음으로 경험해보았습니다.
코드를 수정하면서 원래 됐던 케이스가 안되기도 했는데
이런 걸 쉽게 확인할 수 있어서 유용했어요.

그리고 처음부터 제대로된 테스트 코드를 짜는 것이 생각보다 어렵더라고요.
놓친 테스트 케이스가 있어서 중간에 추가하기도 했고,
테스트 코드에 '='을 넣는 걸 깜빡해서 나중에 한꺼번에 추가하기도 했습니다.
코드를 작성하기 이전에 테스트 케이스를 명확히하고, 테스트 코드 구현도 섬세히 해야겠습니다.

  • cypress 설치
  • typescript 환경 구성
  • 테스트 코드 구현
  • calculator 구현
    • 숫자 입력받기
    • 입력받은 숫자 띄워주기
    • 덧셈 기능
    • 뺄셈 기능
    • 곱셈 기능
    • 나눗셈 기능
    • AC 기능
    • 3자리 초과 예외처리

Copy link
Member

@hochan222 hochan222 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parsedInput class 및 다른 함수들도 기능별로 잘 나눠져있어서 코드를 읽는데 좋았습니다. 코드가 굉장히 깔끔하네요! 많이 배워가요! 고생하셨습니다 :)

"extends": [
"plugin:cypress/recommended"
]
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL 아티클 읽어보시면 좋을것 같아요!

num2: string;
operator: string;

constructor(operator: string, num1: string, num2: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
constructor(operator: string, num1: string, num2: string) {
constructor(operator: string, num1: string, num2: string):void {

typescript인 만큼 void일때도 명시해주는게 가독성을 위해 좋다고 생각합니다.

Comment on lines +28 to +29
const total = document.getElementById("total") as HTMLElement;
let prompt = total.innerHTML as string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const total = document.getElementById("total") as HTMLElement;
let prompt = total.innerHTML as string;
const total: HTMLElement = document.getElementById("total") as HTMLElement;
let prompt: string = total.innerHTML as string;

저는 적는게 좋다고 생각하는데 혹시 어떻게 생각하시나요?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 빠뜨린 부분이 있는데 적어주는게 좋다고 생각합니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hochan222 @jwon42 뒤에 강제 타입캐스팅을 해놓더라도 가독성 측면에서 앞에 있는 게 좋다는 의미죠?

Copy link
Member

@hochan222 hochan222 Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yechoi42 둘이 다른 의미라고 생각합니다. c 언어로 치자면

HTMLElement total = (HTMLElement)document.getElementById("total");

따라서, total 뒤에 붙는 HTMLElement는 형변환 의미가 아닌 변수 선언의 Type의미를 가져서 생략하면 안된다고 생각합니다.

}

function showInput(str: string) {
const prompt= document.getElementById("total") as HTMLElement;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const prompt= document.getElementById("total") as HTMLElement;
const prompt = document.getElementById("total") as HTMLElement;

}

function setOperationsController() {
const operations = document.getElementsByClassName("operations");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분도 타입 명시를 해주면 좋을것 같아요!

const oldText = prompt.innerHTML;
if (prompt.dataset.type === "result") {
console.log(str);
prompt.innerHTML = str;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prompt.innerHTML = str;
prompt.innerHTML = str;

innerHTML은 비용이 굉장히 큰 함수라서 (하위 돔 파괴 생성 및 이벤트까지 없애줌) text만 다루는거면 innerText를 사용하는게 좋아보여요!

Copy link

@yhshin0 yhshin0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

과제 요구사항을 잘 만족하셨습니다. cypress.json 파일을 활용한 것도 인상적이었습니다.

다만 음수가 첫번째 숫자로 들어오는 경우, 두번째 숫자가 세자리를 넘길 수 있는게 조금 아쉽네요.

수고하셨습니다!

}

function checkInputValidation(input: string, str: string): boolean {
let newStr: string = str[0] === "-" ? str.slice(1, prompt.length) : str;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 prompt는 어떤 역할인가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

total 창에 띄워진 문자열인데요, 스코프를 적절하지 않게 사용한 것 같네요. 보완해야 할 부분인 것 같습니다.

cy.get("#total").should("have.text", 9 + 2);
});
it("add one digit numbers: -9 + 2", () => {
cy.reload();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reload는 새로고침인가보네요. 하나 배워갑니다 :)

</div>
</div>
</body>
<script type="module" src="src/ts/index.js"></script>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsconfig.json에서는 컴파일된 js 파일이 src/js/ 경로에 위치하는데 index.html에서는 src/ts/ 경로의 파일을 참조하고 있네요


function parseInput(str: string): parsedInput {
const total = document.getElementById("total") as HTMLElement;
let prompt = total.innerHTML as string;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 프로그램에서 window.prompt() 함수를 사용하진 않지만, prompt 보다는 다른 변수명을 사용하는 것이 좋아 보입니다.

Copy link
Member

@jwon42 jwon42 Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예약어 느낌이군요. 저도 배워갑니다.

Copy link
Member

@jwon42 jwon42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

데이터 속성을 사용해서 result를 체크하는게 좋았습니다.

우린 그냥 c언어도 아닌 42의 많은 제약이 있는 c언어로 코딩을 시작해서 그런지 무언가 필요하면 만들고 보는 습관이 있는데 자바스크립트에선 대부분의 것들이 구현되어있다는 가정 하에 검색을 많이 해봐야 할 것 같아요.

수고하셨습니다 :)

Comment on lines +28 to +29
const total = document.getElementById("total") as HTMLElement;
let prompt = total.innerHTML as string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 빠뜨린 부분이 있는데 적어주는게 좋다고 생각합니다.

}
}

function findOperator(str: string): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string includes 메서드 혹은 정규표현식의 test 혹은 search를 이용하면 더 간단하게 연산자를 추출할 수 있을 듯 합니다.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/includes

Comment on lines +60 to +73
function isOperator(input: string): boolean {
switch (input) {
case "/":
return true;
case "X":
return true;
case "-":
return true;
case "+":
return true;
default:
return false;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분도 정규표현식 혹은 string includes를 이용하면 더 간결할 듯 합니다!

operator === "none" ? [prompt, "1"] : prompt.split(operator);
return new parsedInput(
operator,
startWithMinus ? "-" + nums[0] : nums[0],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- 부호를 위한 예외처리군요. 플래그체크와 삼항연산자라니 c언어를 보는 것 같습니다 😁

function showInput(str: string) {
const prompt= document.getElementById("total") as HTMLElement;
const oldText = prompt.innerHTML;
if (prompt.dataset.type === "result") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

적절한 곳에 데이터속성 사용 좋네요.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants