<commit_before>#!/usr/bin/python2.4
#
# Copyright 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License')
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Rich Text Test Definitions."""

__author__ = '<EMAIL> (<NAME>)'

import logging

from categories import test_set_base


_CATEGORY = 'richtext'


class RichtextTest(test_set_base.TestBase):
  TESTS_URL_PATH = '/%s/test' % _CATEGORY

  def __init__(self, key, name, doc, is_hidden_stat=True, category=None,
               score_type='boolean'):
    """Initialze a test.

    Args:
      key: key for this in dict's
      name: a human readable label for display
      url_name: the name used in the url
      score_type: 'boolean' or 'custom'
      doc: a description of the test
      value_range: (min_value, max_value) as integer values
      is_hidden_stat: whether or not the test shown in the stats table
      category: the category(aka non-hidden test) this test belongs to.
      score_type: string, boolean or custom.
    """
    self.is_hidden_stat = is_hidden_stat
    # This way we can assign tests to a test group, i.e. apply, unapply, etc..
    self.category = category
    test_set_base.TestBase.__init__(
        self,
        key=key,
        name=name,
        url=self.TESTS_URL_PATH,
        score_type=score_type,
        doc=doc,
        min_value=0,
        max_value=1)


  def GetScoreAndDisplayValue(self, median, user_agent, params=None,
                              is_uri_result=False):
    """Custom scoring function.

    Args:
      median: The actual median for this test from all scores
    Returns:
      (score, display)
      Where score is a value between 1-100.
      And display is the text for the cell.
    """
    #logging.info('RichTextTest.GetScoreAndDisplayValue '
    #             'test: %s, median: %s' % (self.key, median))

    tests_in_category = self.GetTestsByCategory(self.key)
    if is_uri_result:
      display_score = int(median)
    else:
      display_score = 0
      for test in tests_in_category:
        test_median = test.GetRanker(user_agent, params).GetMedian()
        if test_median is None:
          test_median = 0
        #logging.info('test_median: %s' % test_median)
        display_score += test_median

    num_tests = len(tests_in_category)
    score = int(display_score / num_tests)
    display = '%s/%s' % (display_score, num_tests)
    return score, display

  def GetTestsByCategory(self, category):
    tests = []
    for test in _TESTS:
      if test.category == category:
        tests.append(test)
    return tests

_TESTS = (
  # key, name, doc
  RichtextTest('apply', 'Apply Formatting', '''About this test...''',
               is_hidden_stat=False, score_type='custom'),
  RichtextTest('unapply', 'Un-Apply Formatting', '''About this test...''',
               is_hidden_stat=False, score_type='custom'),
  RichtextTest('change', 'Change Existing Formatting', '''About this test...''',
               is_hidden_stat=False, score_type='custom'),
  RichtextTest('query', 'Query State and Value', '''About this test...''',
               is_hidden_stat=False, score_type='custom'),<commit_msg>Added descriptions for rich text categories<commit_after>#!/usr/bin/python2.4
#
# Copyright 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License')
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Rich Text Test Definitions."""

__author__ = '<EMAIL> (<NAME>)'

import logging

from categories import test_set_base


_CATEGORY = 'richtext'


class RichtextTest(test_set_base.TestBase):
  TESTS_URL_PATH = '/%s/test' % _CATEGORY

  def __init__(self, key, name, doc, is_hidden_stat=True, category=None,
               score_type='boolean'):
    """Initialze a test.

    Args:
      key: key for this in dict's
      name: a human readable label for display
      url_name: the name used in the url
      score_type: 'boolean' or 'custom'
      doc: a description of the test
      value_range: (min_value, max_value) as integer values
      is_hidden_stat: whether or not the test shown in the stats table
      category: the category(aka non-hidden test) this test belongs to.
      score_type: string, boolean or custom.
    """
    self.is_hidden_stat = is_hidden_stat
    # This way we can assign tests to a test group, i.e. apply, unapply, etc..
    self.category = category
    test_set_base.TestBase.__init__(
        self,
        key=key,
        name=name,
        url=self.TESTS_URL_PATH,
        score_type=score_type,
        doc=doc,
        min_value=0,
        max_value=1)


  def GetScoreAndDisplayValue(self, median, user_agent, params=None,
                              is_uri_result=False):
    """Custom scoring function.

    Args:
      median: The actual median for this test from all scores
    Returns:
      (score, display)
      Where score is a value between 1-100.
      And display is the text for the cell.
    """
    #logging.info('RichTextTest.GetScoreAndDisplayValue '
    #             'test: %s, median: %s' % (self.key, median))

    tests_in_category = self.GetTestsByCategory(self.key)
    if is_uri_result:
      display_score = int(median)
    else:
      display_score = 0
      for test in tests_in_category:
        test_median = test.GetRanker(user_agent, params).GetMedian()
        if test_median is None:
          test_median = 0
        #logging.info('test_median: %s' % test_median)
        display_score += test_median

    num_tests = len(tests_in_category)
    score = int(display_score / num_tests)
    display = '%s/%s' % (display_score, num_tests)
    return score, display

  def GetTestsByCategory(self, category):
    tests = []
    for test in _TESTS:
      if test.category == category:
        tests.append(test)
    return tests

_TESTS = (
  # key, name, doc
  RichtextTest('apply', 'Apply Formatting',
  '''These tests use execCommand to apply formatting to plain text.
  They simply run the execCommand and check if any HTML is generated around the
  selected text. They do not make a judgement call as to what the correct HTML
  should be, only that the given visual style is applied. So &lt;b&gt;,
  &lt;STRONG&gt;, and &lt;span style="font-weight:bold"&gt; are all considered
  valid output of the bold execCommand. The reason for this is that these tests
  are for WYSIWYG editing, not semantic editing. There are many execCommands
  which are not tested here; only the most commonly used commands for rich
  text editing are included. The output of the execCommand is shown in the last
  column of the test output.''',
  is_hidden_stat=False, score_type='custom'),
  RichtextTest('unapply', 'Un-Apply Formatting',
  '''These tests put different combinations of HTML into a contenteditable
  iframe, and then run an execCommand to attempt to remove the formatting the
  HTML applies. For example, there are tests to check if
  bold styling from &lt;b&gt;, &lt;strong&gt;, and &lt;span
  style="font-weight:normal"&gt; are all removed by the bold execCommand.
  It is important that browsers can remove all variations of a style, not just
  the variation the browser applies on its own, because it's quite possible
  that a web application could allow editing with multiple browsers, or that
  users could paste content into the contenteditable region.
  These tests currently only run with styleWithCSS set to false. Any HTML
  which remains after the execCommand runs is shown in the last column of the
  test output.''',
  is_hidden_stat=False, score_type='custom'),
  RichtextTest('change', 'Change Existing Formatting',
  '''These tests are similar to the unapply tests, except that they're for
  execCommands which take an argument (fontname, fontsize, etc.). They apply
  the execCommand to text which already has some formatting, in order to change
  it. After the execCommand runs, the tests find the text node which was
  selected, and climb up its ancestor chain to check that the given formatting
  is only applied once, and that it's the new format and not the old one. The
  last column of the test output shows the resulting HTML after applying the
  execCommand.''',
  is_hidden_stat=False, score_type='custom'),
  RichtextTest('query', 'Query State and Value',
  '''These tests run queryCommandState (for execCommands with no argument)
  and queryCommandValue (for execCommands with an argument) on HTML with
  various types of formatting.''',
  is_hidden_stat=False, score_type='custom'),
{'fraction_non_alphanumeric': 0.08547557840616966, 'fraction_numerical': 0.0038560411311053984, 'mean_word_length': 3.277141548327989, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5978144', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2656, 'n_words': 1023}