ruby 用于测试博客帖子的Ruby代码。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 用于测试博客帖子的Ruby代码。相关的知识,希望对你有一定的参考价值。

require_relative "test_helper"

class TodayTest < Blog::Test
  def test_doesnt_start_with_today
    posts.each do |post|
      content = File.read(post)

      body_text = content.split("\n").delete_if do |line|
        line[0] == "#" || line.strip == ""
      end

      refute body_text.first =~ /Today/, "Posts usually shouldn't start with \"Today\""
    end
  end
end
require "fileutils"

require "rubygems"
require "bundler/setup"
require "minitest/autorun"
require "fastimage"
require "nokogiri"
require "redcarpet"

cat = <<-'cat'



                                                   .--.
                                                   `.  \
                                                     \  \
                                                      .  \
                                                      :   .
                                                      |    .
                                                      |    :
                                                      |    |
      ..._  ___                                       |    |
     `."".`''''""--..___                              |    |
     ,-\  \             ""-...__         _____________/    |
     / ` " '                    `""""""""                  .
     \                                                      L
     (>                                                      \
    /                                                         \
    \_    ___..---.                                            L
      `--'         '.                                           \
                     .                                           \_
                    _/`.                                           `.._
                 .'     -.                                             `.
                /     __.-Y     /''''''-...___,...--------.._            |
               /   _."    |    /                ' .      \   '---..._    |
              /   /      /    /                _,. '    ,/           |   |
              \_,'     _.'   /              /''     _,-'            _|   |
                      '     /               `-----''               /     |
                      `...-'                                       !_____)


cat

puts cat
puts "
  Hello you lovely GitHub blog author person human!
  This is test output from your blog post you just wrote. Unlike most tests in
  life (and code!), it's *totally cool* to fail these tests. They're just here
  to catch anything you might have missed while writing your draft.
  Consider them suggestions rather than things you *need* to do before
  publishing your post.
  Happy shipping!
"

module Blog
  class Test < MiniTest::Test
    # All the posts we're interested in checking. This means we're looking at
    # files that have changed on this particular branch we're on.
    #
    # Returns an Array of String filenames.
    def posts
      diffable_files = `git diff --name-only --diff-filter=ACMRTUXB origin/master... | grep .md`.split("\n")

      posts = diffable_files.select do |filename|
        File.ctime(filename) > Time.new(2014,9,3)
      end

      posts
    end
  end
end
require_relative "test_helper"

require "open-uri"
require "net/http"

class ImagesTest < Blog::Test
  def setup
    @images = posts.map do |post|
      content = File.read(post)
      md      = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
      html    = md.render(content)
      doc     = Nokogiri::HTML(html)

      doc.css("img").map do |img|
        url      = img.attribute("src").value
        uri      = URI(url)
        response = Net::HTTP.get_response(uri)

        { :url => url, :response => response, :element => img }
      end
    end.flatten
  end

  def test_images_arent_too_big
    large_images = @images.select { |img| img[:response].body.size.to_i > 1_000_000 }

    error_message = large_images.map do |img|
      "  #{img[:response].body.size} bytes: #{img[:url]}"
    end.join("\n")

    assert large_images.empty?,
      "The following images are over a megabyte in size... consider shrinking them:\n\n#{error_message}"
  end

  def test_images_have_alt_tags
    @images.select do |img|
      alt = img[:element].attribute("alt").value
      no_alt_tags = (alt.strip == "" || (alt =~ /^screen shot /))

      refute no_alt_tags,
        "Images need an alt tag for accessibility. Your image should have a short, one or two word description of the image in the ![] parts of your post."
    end
  end

  def test_images_are_dotcom_hosted
    @images.select do |img|
      url = img[:url]

      assert_match /^https:\/\/cloud\.githubusercontent\.com/, url,
        "Images should be hosted on GitHub. Try dragging an image into an issue comment, uploading it, and using that URL."
    end
  end

  def test_image_proportions
    @images.select do |img|
      width, height = FastImage.size(img[:url])

      assert width >= 1354,
        "Screenshots should be retina-quality — meaning at least 1354 pixels across — so they look good on higher-resolution screens. If you're taking a shot of a small interface element, consider zooming the page with cmd+plus and taking a screenshot that way; the blog will scale it down for you."

      assert (width/height.to_f) > 1.2,
        "Screenshots should be roughly panoramic (more than a 1.2 width:height ratio). Try taking a wider, shorter screenshot so it reduces the amount of scoll necessary to view the content of the blog post."
    end
  end
end
require_relative "test_helper"

require "open-uri"
require "net/http"

class EmojiTest < Blog::Test
  def test_no_emoji
    posts.each do |post|
      content = File.read(post)
      refute_match /:[a-zA-Z0-9_]+:/, content,
        "You have emoji in your post, which we try to avoid"
    end
  end
end

以上是关于ruby 用于测试博客帖子的Ruby代码。的主要内容,如果未能解决你的问题,请参考以下文章

ruby 我用来将我的Wordpress博客中的帖子导入到新的Jekyll中的脚本。

使用 Ruby on Rails 一键删除所有未批准的帖子

在 Ruby on Rails 中为帖子添加标签

ruby 示例如何在Camaleon CMS中按代码添加帖子

如何在 Ruby 2.2 上运行现有的测试代码

Ruby on Rails 实时活动通知