<commit_before>package org.tribbloid.spookystuff.expression

import org.tribbloid.spookystuff.SpookyEnvSuite
import org.tribbloid.spookystuff.actions.Wget
import org.tribbloid.spookystuff.entity.PageRow
import org.tribbloid.spookystuff.expressions.NamedFunction1

/**
 * Created by peng on 12/3/14.
 */
class TestExprView extends SpookyEnvSuite {

  import org.tribbloid.spookystuff.dsl._

  lazy val page = (
    Wget("http://www.wikipedia.org/") :: Nil
  ).resolve(spooky).toArray
  lazy val row = PageRow(pageLikes = page)
    .select(S"title".head.text ~ 'abc)
    .head

  test("symbol as Expr"){
    assert('abc.apply(row) === Some("Wikipedia"))
  }

  test("defaultAs should not rename an Alias") {
    val renamed = 'abc as 'name1
    assert(renamed.name == "name1")
    val renamed2 = renamed as 'name2
    assert(renamed2.name == "name2")
    val notRenamed = renamed defaultAs 'name2
    assert(notRenamed.name == "name1")
  }

  test("andThen"){
    val fun = 'abc.andThen(_.map(_.toString))
    assert(fun.name === "abc.<function1>")
    assert(fun(row) === Some("Wikipedia"))

    val fun2 = 'abc.andThen(NamedFunction1(_.map(_.toString),"after"))
    assert(fun2.name === "abc.after")
    assert(fun(row) === Some("Wikipedia"))
  }

  test("andMap"){
    val fun = 'abc.andMap(_.toString)
    assert(fun.name === "abc.<function1>")
    assert(fun(row) === Some("Wikipedia"))

    val fun2 = 'abc.andMap(_.toString, "after")
    assert(fun2.name === "abc.after")
    assert(fun(row) === Some("Wikipedia"))
  }

  test("andFlatMap"){
    val fun = 'abc.andFlatMap(_.toString.headOption)
    assert(fun.name === "abc.<function1>")
    assert(fun(row) === Some('W'))

    val fun2 = 'abc.andFlatMap(_.toString.headOption, "after")
    assert(fun2.name === "abc.after")
    assert(fun(row) === Some('W'))
  }
}<commit_msg>add a test to check compatibility of double quotes in selector by attribute in jsoup<commit_after>package org.tribbloid.spookystuff.expression

import org.tribbloid.spookystuff.SpookyEnvSuite
import org.tribbloid.spookystuff.actions.Wget
import org.tribbloid.spookystuff.entity.PageRow
import org.tribbloid.spookystuff.expressions.NamedFunction1

/**
 * Created by peng on 12/3/14.
 */
class TestExprView extends SpookyEnvSuite {

  import org.tribbloid.spookystuff.dsl._

  lazy val page = (
    Wget("http://www.wikipedia.org/") :: Nil
  ).resolve(spooky).toArray
  lazy val row = PageRow(pageLikes = page)
    .select(S"title".head.text ~ 'abc)
    .head

  test("symbol as Expr"){
    assert('abc.apply(row) === Some("Wikipedia"))
  }

  test("defaultAs should not rename an Alias") {
    val renamed = 'abc as 'name1
    assert(renamed.name == "name1")
    val renamed2 = renamed as 'name2
    assert(renamed2.name == "name2")
    val notRenamed = renamed defaultAs 'name2
    assert(notRenamed.name == "name1")
  }

  test("andThen"){
    val fun = 'abc.andThen(_.map(_.toString))
    assert(fun.name === "abc.<function1>")
    assert(fun(row) === Some("Wikipedia"))

    val fun2 = 'abc.andThen(NamedFunction1(_.map(_.toString),"after"))
    assert(fun2.name === "abc.after")
    assert(fun(row) === Some("Wikipedia"))
  }

  test("andMap"){
    val fun = 'abc.andMap(_.toString)
    assert(fun.name === "abc.<function1>")
    assert(fun(row) === Some("Wikipedia"))

    val fun2 = 'abc.andMap(_.toString, "after")
    assert(fun2.name === "abc.after")
    assert(fun(row) === Some("Wikipedia"))
  }

  test("andFlatMap"){
    val fun = 'abc.andFlatMap(_.toString.headOption)
    assert(fun.name === "abc.<function1>")
    assert(fun(row) === Some('W'))

    val fun2 = 'abc.andFlatMap(_.toString.headOption, "after")
    assert(fun2.name === "abc.after")
    assert(fun(row) === Some('W'))
  }

  test("double quotes in selector by attribute should work") {
    lazy val page = (
      Wget("http://www.wikipedia.org/") :: Nil
      ).resolve(spooky).toArray
    lazy val row = PageRow(pageLikes = page)
      .select(S"""[href*="wikipedia"]""".href ~ 'uri)
      .head

    assert(row.get("uri").nonEmpty)
  }
}
{'fraction_non_alphanumeric': 0.17226061204343535, 'fraction_numerical': 0.011846001974333662, 'mean_word_length': 3.7237762237762237, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3022850', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1432, 'n_words': 265}